Skip to content

Instantly share code, notes, and snippets.

@ItsPepperpot
Created March 14, 2018 20:49
Show Gist options
  • Save ItsPepperpot/2fc40c6d846a62c36b655add0807e647 to your computer and use it in GitHub Desktop.
Save ItsPepperpot/2fc40c6d846a62c36b655add0807e647 to your computer and use it in GitHub Desktop.
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using WinChessGUI.Properties;
using Color = System.Drawing.Color;
namespace WinChessGUI
{
/// <summary>
/// Used to display a chess board.
/// </summary>
[ToolboxBitmap(@"Board.bmp")] // TODO: Fix icon for toolbox.
public partial class Board : UserControl
{
private Panel[,] _squares;
/// <summary>
/// Initializes a new instance of the Board class.
/// </summary>
public Board()
{
InitializeComponent();
_squares = new Panel[8, 8];
SelectedSquareX = SelectedSquareY = HighlightedSquareX = HighlightedSquareY = -1;
// Initializes the colors for hovering and selecting squares TODO: Remove this in future?
SelectedSquareColor = Color.FromArgb(255, 40, 255, 87);
HighlightedSquareColor = Color.FromArgb(255, 141, 252, 165);
// Disables double clicking on the control.
SetStyle(ControlStyles.StandardDoubleClick, false);
// Prevents the control from flickering when updating.
DoubleBuffered = true;
InitializeSquares();
}
/// <summary>
/// Initializes the panels.
/// </summary>
private void InitializeSquares()
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
_squares[i, j] = new Panel();
_squares[i, j].Width = Width / 8;
_squares[i, j].Height = Height / 8;
_squares[i, j].Left = i * (Width / 8);
_squares[i, j].Top = j * (Height / 8);
if (i % 2 == 0 && j % 2 == 0 || i % 2 == 1 && j % 2 == 1)
_squares[i, j].BackColor = LightSquareColor;
else
_squares[i, j].BackColor = DarkSquareColor;
Controls.Add(_squares[i, j]);
}
}
}
private void Board_Paint(object sender, PaintEventArgs e)
{
// TODO: Repaint algorithm with selected/hovered squares
}
/// <summary>
/// The color for the dark squares on the board.
/// </summary>
[Category("Appearance"), Description("The color for the dark squares on the board.")]
public Color DarkSquareColor { get; set; }
/// <summary>
/// The color for the light squares on the board.
/// </summary>
[Category("Appearance"), Description("The color for the light squares on the board.")]
public Color LightSquareColor { get; set; }
/// <summary>
/// The color for the currently selected square on the board.
/// </summary>
[Category("Appearance"), Description("The color for the currently selected square on the board.")]
public Color SelectedSquareColor { get; set; }
/// <summary>
/// The color for the highlighted square on the board.
/// </summary>
[Category("Appearance"), Description("The color for the highlighted square on the board.")]
public Color HighlightedSquareColor { get; set; }
/// <summary>
/// The X-coordinate of the selected square.
/// </summary>
public int SelectedSquareX { get; set; }
/// <summary>
/// The Y-coordinate of the selected square.
/// </summary>
public int SelectedSquareY { get; set; }
/// <summary>
/// The X-coordinate of the hovered square.
/// </summary>
public int HighlightedSquareX { get; set; }
/// <summary>
/// The Y-coordinate of the hovered square.
/// </summary>
public int HighlightedSquareY { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment