-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridPainter.cs
More file actions
123 lines (103 loc) · 3.82 KB
/
GridPainter.cs
File metadata and controls
123 lines (103 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) 2012-2026 The Hello World Writer (https://www.thehelloworldwriter.com).
// Licensed under the MIT License. See the LICENSE file in the project root for more information.
namespace GridlyDots
{
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
/// <summary>Draws a grid on a surface.</summary>
public class GridPainter
{
/// <summary>Indicates whether the grid properties have been changed.</summary>
private bool gridChanged = true;
/// <summary>The texture brush used to draw the grid.</summary>
private Brush gridBrush = null!;
/// <summary>The size of the grid dot, in pixels.</summary>
private int dotSize = 5;
/// <summary>The spacing between grid dots, in pixels.</summary>
private int dotSpacing = 15;
/// <summary>The color of the grid dots.</summary>
private Color dotColor = Color.Red;
/// <summary>Gets or sets the size of the grid dot, in pixels.</summary>
public int DotSize
{
get
{
return this.dotSize;
}
set
{
this.dotSize = value;
this.gridChanged = true;
}
}
/// <summary>Gets or sets the spacing between grid dots, in pixels.</summary>
public int DotSpacing
{
get
{
return this.dotSpacing;
}
set
{
this.dotSpacing = value;
this.gridChanged = true;
}
}
/// <summary>Gets or sets the color of the grid dots.</summary>
public Color DotColor
{
get
{
return this.dotColor;
}
set
{
this.dotColor = value;
this.gridChanged = true;
}
}
/// <summary>
/// Draws the grid on the specified drawing surface.
/// </summary>
/// <param name="graphics">The drawing surface where to draw the grid.</param>
/// <param name="area">The area where to draw the grid.</param>
public void Draw(Graphics graphics, Rectangle area)
{
ArgumentNullException.ThrowIfNull(graphics);
// If the grid properties changed, remake the grid brush
if (this.gridChanged || (this.gridBrush == null))
{
if (this.gridBrush != null)
{
this.gridBrush.Dispose();
this.gridBrush = null!;
}
this.gridBrush = this.MakeGridBrush();
}
// Fill the area with the grid
graphics.FillRectangle(this.gridBrush, area);
}
/// <summary>
/// Creates and returns the grid texture brush, based on the current grid properties.
/// </summary>
/// <returns>The texture brush that will be used to fill the grid.</returns>
private TextureBrush MakeGridBrush()
{
// The width and height of the grid texture bitmap (dot size + dot space)
int size = this.DotSize + this.DotSpacing;
// Create and fill the grid texture bitmap
using Bitmap bitmap = new(size, size);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
using Brush dotBrush = new SolidBrush(this.DotColor);
// Set high quality rendering for smaller dots
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
// Draw the pixel as a round circle
graphics.FillEllipse(dotBrush, 0, 0, this.DotSize, this.DotSize);
}
// Return the grid texture brush
return new TextureBrush(bitmap);
}
}
}