Ktoś mi napisał że mam wszystko za bardzo rozrzucone, nie studiuje informatyki ani nie pracuje jako programista wiec nie mam możliwości weryfikacji. Jakby ktoś wskazał mi błedy i rzeczy do poprawy to nie żałował bym plusów :P
To jest kontrolka robiąca jako plansza do warcab, załóżmy że nawet działa. PawnMap to jest mapa Pionków nakładka na tablice 2d, Queen, Pawn, EmptyCell to pionki na wspólnym interfejsie, Move to obiekt wykonujący ruch na PawnMap,"From" to pole z którego ruch się zaczyna, a "To" to jest pole na którym się kończy. Przechowuje co prawda jeszcze jakieś informacje ale ale z punktu widzenia grafiki to nie istotne - może przez to wypadało by go czyms zastąpić nwm, jest jak jest :P. To że używam 2 różnych rodzaj klas Point wynika z tego że MinMax2 i MinMax2 są z innego projektu i tam tak jest. To chyba wszystko:).
Plansza ma wymiar 8x8 pól stąd ta magiczna 8.
using MinMax2;
using MinMax2.Pawns;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PointSource = System.Drawing;
namespace WarcabyGrafika
{
/// <summary>
/// Interaction logic for Plansza.xaml
/// </summary>
public partial class Plansza : UserControl
{
private readonly BitmapImage WHITE_QUEEN= new BitmapImage(new Uri("pack://application:,,,/img/HetmanB.png"));
private readonly BitmapImage WHITE_PAWN = new BitmapImage(new Uri("pack://application:,,,/img/PionekB.png"));
private readonly BitmapImage BLACK_PAWN = new BitmapImage(new Uri("pack://application:,,,/img/PionekC.png"));
private readonly BitmapImage BLACK_QUEEN = new BitmapImage(new Uri("pack://application:,,,/img/HetmanC.png"));
IList<Move> _posibleMoves = new List<Move>();
Image[,] _imgs = new Image[8, 8];
private Image _movingImg = new Image();
PawnMap _lastMove = new PawnMap(8);
bool _isMoving = false;
PointSource.Point _from = new PointSource.Point(-1, -1);
public event Action<PointSource.Point, PointSource.Point> MoveSelected;
public Plansza()
{
InitializeComponent();
_imgs = InitImages();
_from = new PointSource.Point(-1, -1);
this.MouseLeave += BreakMoving;
this.PreviewMouseLeftButtonUp += StopMoving;
this.PreviewMouseMove += PawnMoved;
_movingImg.Source = WHITE_PAWN;
_movingImg.Opacity = 0.7;
this.playGround.Children.Add(_movingImg);
}
#region Initalize private
private Image[,] InitImages()
{
Image[,] imgs = new Image[8, 8];
for (int y = 0; y < 8; y++)
{
for (int x = 0; x < 8; x++)
{
imgs[x, y] = new Image();
imgs[x, y].PreviewMouseDown += StartMoving;
SetPostionOnMap(imgs[x, y], x, y);
playGround.Children.Add(imgs[x, y]);
}
}
return imgs;
}
private void SetPostionOnMap(FrameworkElement imgs, int x, int y)
{
const int marginX = 3;
const int marginY = 3;
imgs.Margin = new Thickness(marginX, marginY, 0, 0);
Canvas.SetLeft(imgs, FieldWight * x);
Canvas.SetTop(imgs, FieldHeight * y);
}
#endregion
public double FieldWight { get { return playGround.Width / 8; } }
public double FieldHeight { get { return playGround.Height / 8; } }
public void UpDate(PawnMap map, IList<Move> moves)
{
if (map.IsNull() || moves.IsNull())
throw new ArgumentNullException("Map and null list can not neither be null or empty");
_posibleMoves = moves;
DrawMap(map);
_lastMove = map.Copy();
}
private void DrawMap(PawnMap map)
{
for (int y = 0; y < 8; y++)
{
for (int x = 0; x < 8; x++)
{
if (map.GetAt(x, y) != _lastMove.GetAt(x, y))
{
_imgs[x, y] = DrawPawn(_imgs[x, y], map.GetAt(x, y));
}
}
}
}
private Image DrawPawn(Image img, IMan man)
{
if (man == Queen.WhiteInstance)
img.Source = WHITE_QUEEN;
else if (man == Queen.WhiteInstance)
img.Source = BLACK_QUEEN;
else if (man == Pawn.WhiteInstance)
img.Source = WHITE_PAWN;
else if (man == Pawn.BlackInstance)
img.Source = BLACK_PAWN;
else if (man == PawnMap.EmptyCell)
{ img.Visibility = System.Windows.Visibility.Collapsed; return img; }
else
throw new NotImplementedException("Unknow pawn Type");
img.Visibility = System.Windows.Visibility.Visible;
img.UpdateLayout();
return img;
}
#region Mouse moving and selecting
private void StartMoving(object sender, MouseButtonEventArgs e)
{
var p = GetPawnFromCoords(e.GetPosition(this.playGround));
if (!_lastMove.IsInRage(p))
return;
if (_posibleMoves.Any(m => m.To.Equals(p)))
return;
_isMoving = true;
_movingImg = DrawPawn(_movingImg, _lastMove.GetAt(p));
_from = p;
}
private void StopMoving(object sender, MouseButtonEventArgs e)
{
var targetPoint = GetPawnFromCoords(e.GetPosition(playGround));
bool isMovePosible = _posibleMoves.Any(m => m.From.Equals(_from) && m.To.Equals(targetPoint));
if (isMovePosible)
{
if (MoveSelected != null)
MoveSelected.Invoke(_from, targetPoint);
}
BreakMoving(sender, e);
}
private void BreakMoving(object sender, MouseEventArgs e)
{
_isMoving = false;
_movingImg.Visibility = System.Windows.Visibility.Collapsed;
_from = new PointSource.Point(-1, -1);
Cursor = Cursors.Arrow;
}
private void PawnMoved(object sender, MouseEventArgs e)
{
if (_isMoving)
{
_movingImg.Visibility = System.Windows.Visibility.Visible;
Canvas.SetLeft(_movingImg, e.GetPosition(playGround).X - 0.5 * FieldWight);
Canvas.SetTop(_movingImg, e.GetPosition(playGround).Y - 0.5 * FieldHeight);
}
Cursor = SelectCursorView(e.GetPosition(this.playGround));
}
private Cursor SelectCursorView(Point point)
{
var p = GetPawnFromCoords(point);
if (!_isMoving && _posibleMoves.Any(m => m.From.Equals(p)))
return Cursors.Hand;
else if (_isMoving && _posibleMoves.Any(m => m.From.Equals(_from) && m.To.Equals(p)))
return Cursors.Hand;
else if (_isMoving)
return Cursors.No;
else
return Cursors.Arrow;
}
private PointSource.Point GetPawnFromCoords(Point coords)
{
int x = 7 - (int)((playGround.Width - coords.X) / FieldWight);
int y = 7 - (int)((playGround.Height - coords.Y) / FieldHeight);
return new PointSource.Point(x, y);
}
private void GetPawnFromCoords(Point coords, out int x, out int y)
{
x = (int)(coords.X / FieldWight);
y = (int)(coords.Y / FieldHeight);
}
#endregion
}
}