private void ResetDragAndDrop()
 {
     if (_selectedPiece == null)
     {
         return;
     }
     Canvas.Children.Remove(_selectedPiece);
     _clickedSquare.PieceView = _selectedPiece;
     _selectedPiece           = null;
     _hasBeginDragAndDrop     = false;
 }
        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);
            }
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            var p = e.GetPosition(Grid);

            if (p.X < 0 || p.Y < 0 || p.X > Grid.ActualWidth || p.Y > Grid.ActualHeight)
            {
                if (_selectedPiece == null)
                {
                    return;
                }
                Canvas.Children.Remove(_selectedPiece);
                _clickedSquare.Square.Piece = _selectedPiece.Piece;
                _selectedPiece = null;
                _selected      = false;
                return;
            }
            //Use the threshold too
            if (!_mouseDown)
            {
                return;
            }

            if (_selectedPiece == null)
            {
                return;
            }

            if (_initDragAndDropOnMouseMove && (_mouseDownPoint - e.GetPosition(Grid)).Length > 5)
            {
                Console.WriteLine("Init drag and drop");
                _clickedSquare.PieceView = null;

                var width  = _selectedPiece.ActualWidth;
                var height = _selectedPiece.ActualHeight;

                //Put the pieceView in the canvas to be able to drag&drop
                Canvas.Children.Add(_selectedPiece);

                _selectedPiece.Height = height;
                _selectedPiece.Width  = width;

                _initDragAndDropOnMouseMove = false;
                _hasBeginDragAndDrop        = true;
            }

            Canvas.SetTop(_selectedPiece, e.GetPosition(this).Y - _selectedPiece.ActualHeight / 2);
            Canvas.SetLeft(_selectedPiece, e.GetPosition(this).X - _selectedPiece.ActualWidth / 2);
        }
示例#4
0
        public SquareView(Square square)
        {
            InitializeComponent();
            Square                  = square;
            DataContext             = this;
            Square.PropertyChanged += SquarePropertyChangeHandler;

            if (square.Piece != null)
            {
                PieceView = new PieceView(square.Piece);
            }

            SetResourceReference(BackgroundProperty,
                                 (square.X + square.Y) % 2 == 0 ? "MahApps.Brushes.Accent" : "MahApps.Brushes.Accent4");

            Grid.SetColumn(this, square.X);
            Grid.SetRow(this, square.Y);
        }
        protected override void OnMouseUp(MouseButtonEventArgs e)
        {
            base.OnMouseUp(e);

            if (e.ChangedButton != MouseButton.Left)
            {
                return;
            }

            _mouseDown = false;
            _initDragAndDropOnMouseMove = false;

            var concernedControllers = ConcernedControllers();

            if (concernedControllers.Count == 0)
            {
                return;
            }

            Move move;
            var  squareView = SquareAt(e.GetPosition(Grid));

            //TODO Put all the fields in the corresponding state

            if (squareView == null)
            {
                if (_selectedPiece == null)
                {
                    return;
                }

                Canvas.Children.Remove(_selectedPiece);
                _clickedSquare.Square.Piece = _selectedPiece.Piece;
                _selectedPiece = null;
                _selected      = false;

                return;
            }

            var clickedSquareView = SquareAt(_mouseDownPoint);
            var select            = Equals(squareView.Square.Coordinate, clickedSquareView?.Square?.Coordinate); //Same square

            if (select)
            {
                if (_hasBeginDragAndDrop)
                {
                    Canvas.Children.Remove(_selectedPiece);
                    _clickedSquare.PieceView = _selectedPiece;
                    _selectedPiece           = null;
                    _hasBeginDragAndDrop     = false;
                    ResetBoardColor();
                    return;
                }
                //First click
                if (!_selected)
                {
                    Console.WriteLine("Selection");
                    _selected = true;
                }
                //Second click
                else
                {
                    if (!_possibleMoves.Contains(_clickedSquare))
                    {
                        return;
                    }

                    _selected = false;
                    ResetBoardColor();
                    if (_selectedPiece.Piece.Type == Type.Pawn &&
                        squareView.Square.Y == (_selectedPiece.Piece.Color == Color.White ? 0 : 7))
                    {
                        var promoteDialog = new PieceTypeSelectionWindow(_selectedPiece.Piece.Color);
                        promoteDialog.ShowDialog();
                        move = new Move(_selectedPiece.Piece.Square, squareView.Square, _selectedPiece.Piece.Type,
                                        _selectedPiece.Piece.Color, promoteDialog.ChosenType);
                    }
                    else
                    {
                        move = new Move(_selectedPiece.Piece, squareView.Square);
                    }
                    concernedControllers.ForEach(x => x.Move(move));
                }
            }
            else //Drag case
            {
                if (!_possibleMoves.Contains(squareView))
                {
                    ResetDragAndDrop();
                    ResetBoardColor();
                    return;
                }
                Canvas.Children.Remove(_selectedPiece);
                ResetBoardColor();

                if (_selectedPiece.Piece.Type == Type.Pawn &&
                    squareView.Square.Y == (_selectedPiece.Piece.Color == Color.White ? 0 : 7))
                {
                    var promoteDialog = new PieceTypeSelectionWindow(_selectedPiece.Piece.Color);
                    promoteDialog.ShowDialog();
                    move = new Move(_selectedPiece.Piece.Square, squareView.Square, _selectedPiece.Piece.Type,
                                    _selectedPiece.Piece.Color, promoteDialog.ChosenType);
                }
                else
                {
                    move = new Move(_selectedPiece.Piece, squareView.Square);
                }
                concernedControllers.ForEach(x => x.Move(move));
                _selected            = false;
                _hasBeginDragAndDrop = false;
            }
        }