internal void SetBackgroundColor(Color backColor, bool setAll = true) { /* Set the BackColor property of all the squares to <backColor>. * We use this method when we want to change the background color of the grid, * which means that all squares should be set to the same color. * If setAll is true, we set the background colour for all the squares. * If it's false, we only set the background colour for squares * which were not painted. */ // First we set the grid's default background colour to <backColor> this.DefaultBackgroundColor = backColor; // Now we set each square's BackColor property to <backColor> foreach (var squareList in Squares) { foreach (Square squareObj in squareList) { if (!setAll) { // Not all squares should be set. // This means that we want to set the background colour // only for squares which were not painted by the user if (PaintedSquares.ContainsKey(squareObj.Location)) { // This square is painted. We skip it. continue; } } else { // Remove the square from PaintedSquares because we want a clean grid if (PaintedSquares.ContainsKey(squareObj.Location)) { PaintedSquares.Remove(squareObj.Location); } } // Set the background colour the square squareObj.BackColor = backColor; } } // Redraw the grid this.Invalidate(); }
private void ShrinkSquares() { /* Remove squares from the list of squares */ // First we remove entirely deleted lists. // This happens when the height was changed. // We loop backwards from the previous count of squares, // because it's larger than the new size (either height or width). for (int y = Squares.Count - 1; y > this.Height / SquareSideLength; y--) { // Height is in pixels, so we divide it by SquareSideLength to get the list index. foreach (Square squareObj in Squares[y]) { if (PaintedSquares.ContainsKey(squareObj.Location)) { // A non existent square cannot be painted ;) PaintedSquares.Remove(squareObj.Location); } } // Now we can remove the entire sublist. Squares.RemoveAt(y); } // Now we remove any extra squares on each sublist. // This happens when the width was changed. for (int y = 0; y < Squares.Count; y++) { // Once again we loop backwards starting at Count - 1 until we hit the new width. for (int x = Squares[y].Count - 1; x > this.Width / SquareSideLength; x--) { Square squareObj = Squares[y][x]; Squares[y].RemoveAt(x); if (PaintedSquares.ContainsKey(squareObj.Location)) { // Remove this square from the PaintedSquares dictionary as well PaintedSquares.Remove(squareObj.Location); } } } // Redraw the grid this.Invalidate(); }