chess-board/ChessPanel/Field.cs

122 lines
5.2 KiB
C#

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(Field sender);
public event ClickEventHandler FieldClick;
public bool IsEmpty { get { return currentPiece == null; } }
public Piece currentPiece;
public Piece CurrentPiece
{
get
{
return currentPiece;
}
set
{
currentPiece = value;
RefreshPiece();
}
}
public override string Text
{
get
{
return "";
var pos = location.ToString();
if (IsEmpty)
return pos + Environment.NewLine + "leer";
return pos + Environment.NewLine + currentPiece.ToString();
}
}
public 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 ? FieldColorSet.primaryNormalColorSet : FieldColorSet.secondaryNormalColorSet;
this.currentAllyColorSet = (x + y) % 2 == 0 ? FieldColorSet.primaryValidFriendlyTargetFieldSet : FieldColorSet.secondaryValidFriendlyTargetFieldSet;
this.currentEnemyColorSet = (x + y) % 2 == 0 ? FieldColorSet.primaryValidEnemyTargetFieldSet : FieldColorSet.secondaryValidEnemyTargetFieldSet;
this.currentSelectedColorSet = (x + y) % 2 == 0 ? FieldColorSet.primarySelectedColorSet : FieldColorSet.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 = string.Empty;
this.BackgroundImageLayout = ImageLayout.Zoom;
this.BackgroundImage = ChessPanel.Properties.Resources.Empty;
this.Click += Clicked;
}
private void Clicked(object sender, EventArgs e)
{
FieldClick(this);
}
public override string ToString()
{
if (IsEmpty)
return "leer";
return this.currentPiece.ToString();
}
}
}