/// <summary> /// Создаёт новую фигуру и добавляет её на поле. /// </summary> /// <param name="figure">Фигура которую нужно создать.</param> private static void CreateFigure(Figure figure) { FigureLabel figureLabel = new FigureLabel(figure) { Effect = new DropShadowEffect { Color = new Color { A = 0, R = 150, G = 150, B = 150, }, ShadowDepth = 2 } }; figureLabel.MouseDown += CreateMarks; BrushConverter converter = new BrushConverter(); if (figure.IsWhite) { figureLabel.Foreground = (Brush)converter.ConvertFromString("#FFFFFFFF"); } Field.mainGrid.Children.Add(figureLabel); figureLabels.Add(figureLabel); }
/// <summary> /// Создаёт на поле метки показывающие возможные ходы. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void CreateMarks(object sender, RoutedEventArgs e) { DeleteMarks(); var label = (FigureLabel)sender; if (label.Figure.IsWhite == IsWhiteTurn) { List <Cell> cells = label.Figure.GetPossibleTurns(figures); foreach (Cell c in cells) { BrushConverter converter = new BrushConverter(); Label possibleTurn_Label = new Label { HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Top, Margin = new Thickness(c.PosX * cellSize, c.PosY * cellSize, 0, 0), Height = cellSize, Width = cellSize, Background = (Brush)converter.ConvertFromString("#FF2A9224"), Opacity = 0.5, BorderThickness = new Thickness(1) }; possibleTurn_Label.MouseDown += MoveFigure; if (c.IsCastling) { possibleTurn_Label.MouseDown += (send, a) => { foreach (var fLabel in figureLabels) { if (fLabel.Figure is Rook && fLabel.Figure == c.RookForCastling) { fLabel.Cell = ((Rook)fLabel.Figure).CastlingCell; } } }; } bufLabel = label; Field.mainGrid.Children.Add(possibleTurn_Label); possibleTurn_Labels.Add(possibleTurn_Label); } } }
/// <summary> /// Перемещает фигуру на выбранную клетку. /// </summary> private static void MoveFigure(object sender, RoutedEventArgs e) { Label mark = (Label)sender; if (bufLabel != null) { foreach (FigureLabel figureLabel in figureLabels) { if ((mark.Margin.Left / cellSize == figureLabel.Figure.Cell.PosX) && (mark.Margin.Top / cellSize == figureLabel.Figure.Cell.PosY)) { if (figureLabel.Figure is King) { foreach (var control in Field.mainGrid.Children) { if (control is FigureLabel) { ((FigureLabel)control).MouseDown -= CreateMarks; } } new Views.MateWindow().ShowDialog(); DeleteMarks(); return; } RemoveFigure(figureLabel); break; } ; } bufLabel.Margin = mark.Margin; bufLabel.Figure.Cell = new Cell((int)mark.Margin.Left / cellSize, (int)mark.Margin.Top / cellSize); IsWhiteTurn = !bufLabel.Figure.IsWhite; } DeleteMarks(); bufLabel = null; }
/// <summary> /// Удаляет фигуру с поля. /// </summary> /// <param name="figureLabel"></param> private static void RemoveFigure(FigureLabel figureLabel) { Field.mainGrid.Children.Remove(figureLabel); figureLabels.Remove(figureLabel); figures.Remove(figureLabel.Figure); }