protected override void OnMouseDown(MouseButtonEventArgs e) { base.OnMouseDown(e); if (e.ChangedButton != MouseButton.Left) { return; } _mouseDown = true; _mouseDownPoint = e.GetPosition(Grid); _clickedSquare = SquareAt(e.GetPosition(Grid)); if (_clickedSquare == null) { return; } if (_selected && _possibleMoves.Contains(_clickedSquare)) { return; } ResetBoardColor(); _selectedPiece = _clickedSquare.PieceView; //--Verified--// //Concerned controllers to get the possible moves var concernedControllers = BoardViewPlayerControllers.FindAll( x => x.Player.Color == _selectedPiece?.Piece.Color && x.IsPlayable); if (concernedControllers.Count == 0) { return; } _initDragAndDropOnMouseMove = true; //Possible move drawing foreach (var square in concernedControllers.First().PossibleMoves(_selectedPiece.Piece)) { var squareView = Grid.Children.Cast <SquareView>() .First(x => Grid.GetRow(x) == square.Y && Grid.GetColumn(x) == square.X); squareView.SetResourceReference(BackgroundProperty, (square.X + square.Y) % 2 == 0 ? "MahApps.Brushes.Button.CleanWindow.Close.Background.MouseOver" : "MahApps.Brushes.Button.CleanWindow.Close.Background.Pressed"); _possibleMoves.Add(squareView); } }
public void GameStateChanged(BoardState state) { SquareView squareView = null; ResetBoardColor(); switch (state) { case BoardState.Normal: if (_lastChangedSquareView != null) { ResetSquareViewColor(_lastChangedSquareView); } break; case BoardState.WhiteCheck: squareView = SquareViews.First( x => x.Square?.Piece?.Type == Type.King && x.Square?.Piece?.Color == Color.White); squareView.SetResourceReference(BackgroundProperty, "MahApps.Brushes.Validation5"); break; case BoardState.BlackCheck: squareView = SquareViews.First( x => x.Square?.Piece?.Type == Type.King && x.Square?.Piece?.Color == Color.Black); squareView.SetResourceReference(BackgroundProperty, "MahApps.Brushes.Validation5"); break; case BoardState.BlackCheckMate: squareView = SquareViews.First( x => x.Square?.Piece?.Type == Type.King && x.Square?.Piece?.Color == Color.Black); squareView.SetResourceReference(BackgroundProperty, "MahApps.Brushes.Text"); break; case BoardState.WhiteCheckMate: squareView = SquareViews.First( x => x.Square?.Piece?.Type == Type.King && x.Square?.Piece?.Color == Color.White); squareView.SetResourceReference(BackgroundProperty, "MahApps.Brushes.Text"); break; case BoardState.BlackPat: squareView = SquareViews.First( x => x.Square?.Piece?.Type == Type.King && x.Square?.Piece?.Color == Color.Black); squareView.SetResourceReference(BackgroundProperty, "MahApps.Brushes.SystemControlForegroundChromeWhite"); break; case BoardState.WhitePat: squareView = SquareViews.First( x => x.Square?.Piece?.Type == Type.King && x.Square?.Piece?.Color == Color.White); squareView.SetResourceReference(BackgroundProperty, "MahApps.Brushes.SystemControlForegroundChromeWhite"); break; default: throw new ArgumentOutOfRangeException(nameof(state), state, null); } _lastChangedSquareView = squareView; }
private static void ResetSquareViewColor(SquareView squareView) { squareView.SetResourceReference(BackgroundProperty, (squareView.Square.X + squareView.Square.Y) % 2 == 0 ? "MahApps.Brushes.Accent" : "MahApps.Brushes.Accent4"); }
public BoardView(Container container) { InitializeComponent(); Board = container.Board; _container = container; for (var i = 0; i < Board.Size; i++) { Grid.RowDefinitions.Add(new RowDefinition()); Grid.ColumnDefinitions.Add(new ColumnDefinition()); } Grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto }); /* Ajout des cases */ foreach (var square in Board.Squares) { var squareView = new SquareView(square) { UcPieceView = { LayoutTransform = LayoutTransform }, LayoutTransform = LayoutTransform }; SquareViews.Add(squareView); Grid.Children.Add(squareView); //Position is set in the squareview constructor } /* Ajout des cases */ /* Numéro de case */ for (var i = 0; i < Board.Size; i++) { var label = new Label { Content = (char)('A' + i), HorizontalAlignment = HorizontalAlignment.Center }; Grid.SetColumn(label, i); Grid.SetRow(label, 8); Grid.Children.Add(label); } for (var i = Board.Size; i > 0; i--) { var label = new Label { Content = Board.Size - i + 1, VerticalAlignment = VerticalAlignment.Center }; Grid.SetColumn(label, 8); Grid.SetRow(label, i - 1); Grid.Children.Add(label); } /* Numéro de case */ /* Affichage du dernier coup */ _container.Moves.CollectionChanged += (sender, args) => { if (args.Action == NotifyCollectionChangedAction.Add || args.Action == NotifyCollectionChangedAction.Remove) { if (_container.Moves.Count != 0) { Console.WriteLine("Changed color"); _lastMove.ForEach(ResetSquareViewColor); _lastMove.Clear(); var command = _container.Moves.Last(); var startSquare = SquareAt(command.Move.StartCoordinate); var targetSquare = SquareAt(command.Move.TargetCoordinate); _lastMove.Add(targetSquare); _lastMove.Add(startSquare); } } }; }