chess-board/ChessPanel/Field.cs

102 lines
4.6 KiB
C#
Raw Normal View History

2017-02-27 15:15:50 +01:00
using System;
using System.Windows.Forms;
using System.Drawing;
using static Chess.Piece;
using System.Windows;
namespace Chess
{
public class Field : Button
{
static readonly FieldColorSet primaryNormalColorSet = new FieldColorSet(Color.FromArgb(204, 204, 204), Color.FromArgb(153, 153, 153), Color.White);
static readonly FieldColorSet secondaryNormalColorSet = new FieldColorSet(Color.FromArgb(51, 51, 51), Color.FromArgb(102, 102, 102), Color.Black);
static readonly FieldColorSet primarySelectedColorSet = new FieldColorSet(Color.FromArgb(204, 204, 102), Color.FromArgb(153, 153, 102), Color.FromArgb(255, 255, 102));
static readonly FieldColorSet secondarySelectedColorSet = new FieldColorSet(Color.FromArgb(204, 204, 51), Color.FromArgb(153, 153, 51), Color.FromArgb(255, 255, 51));
static readonly FieldColorSet primaryValidFriendlyTargetFieldSet = new FieldColorSet(Color.FromArgb(102, 204, 102), Color.FromArgb(102, 153, 102), Color.FromArgb(102, 255, 102));
static readonly FieldColorSet secondaryValidFriendlyTargetFieldSet = new FieldColorSet(Color.FromArgb(51, 204, 51), Color.FromArgb(51, 153, 51), Color.FromArgb(51, 255, 51));
static readonly FieldColorSet primaryValidEnemyTargetFieldSet = new FieldColorSet(Color.FromArgb(204, 102, 102), Color.FromArgb(153, 102, 102), Color.FromArgb(255, 102, 102));
static readonly FieldColorSet secondaryValidEnemyTargetFieldSet = new FieldColorSet(Color.FromArgb(204, 51, 51), Color.FromArgb(153, 51, 51), Color.FromArgb(255, 51, 51));
public Vector location;
private FieldColorSet currentNormalColorSet;
private FieldColorSet currentEnemyColorSet;
private FieldColorSet currentSelectedColorSet;
private FieldColorSet currentAllyColorSet;
public delegate void ClickEventHandler(Vector coordinates);
public event ClickEventHandler FieldClick;
public bool IsEmpty { get { return currentPiece == null; } }
public Piece currentPiece;
public Piece CurrentPiece
{
get { return currentPiece; }
set { currentPiece = value; RefreshPiece(); }
}
private void RefreshPiece()
{
if (currentPiece == null)
this.BackgroundImage = ChessPanel.Properties.Resources.Empty;
else
this.BackgroundImage = CurrentPiece.image;
}
public void Deselect()
{
setActiveColorSet(currentNormalColorSet);
}
public void Highlight(PieceTeam team) // validTargetField
{
switch (team)
{
case PieceTeam.Enemy:
setActiveColorSet(currentEnemyColorSet);
break;
case PieceTeam.Ally:
setActiveColorSet(currentAllyColorSet);
break;
default:
break;
}
}
public new void Select() // selectedField
{
setActiveColorSet(currentSelectedColorSet);
}
private void setActiveColorSet(FieldColorSet set)
{
this.BackColor = set.NormalColor;
this.FlatAppearance.MouseDownBackColor = set.MouseDownColor;
this.FlatAppearance.MouseOverBackColor = set.MouseOverColor;
}
public Field(int x, int y)
{
this.currentNormalColorSet = (x + y) % 2 == 0 ? primaryNormalColorSet : secondaryNormalColorSet;
this.currentAllyColorSet = (x + y) % 2 == 0 ? primaryValidFriendlyTargetFieldSet : secondaryValidFriendlyTargetFieldSet;
this.currentEnemyColorSet = (x + y) % 2 == 0 ? primaryValidEnemyTargetFieldSet : secondaryValidEnemyTargetFieldSet;
this.currentSelectedColorSet = (x + y) % 2 == 0 ? primarySelectedColorSet : secondarySelectedColorSet;
this.FlatStyle = FlatStyle.Flat;
this.FlatAppearance.BorderSize = 0;
this.setActiveColorSet(this.currentNormalColorSet);
this.Cursor = Cursors.Arrow;
this.location = new Vector(x, y); // NICHT L(!)ocation, der gibt die Position des Buttons in Pixel auf dem Control an!
this.Name = string.Format($"field[{x},{y}]");
this.Text = "";
this.BackgroundImageLayout = ImageLayout.Zoom;
this.BackgroundImage = ChessPanel.Properties.Resources.Empty;
this.Click += clicked;
}
private void clicked(object sender, EventArgs e)
{
FieldClick(this.location);
}
public override string ToString()
{
return this.currentPiece.type.ToString();
}
}
}