-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
258 lines (226 loc) · 8.28 KB
/
MainWindow.xaml.cs
File metadata and controls
258 lines (226 loc) · 8.28 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using System;
using System.Windows;
using System.Windows.Input;
namespace GridExemplarWPF
{
public partial class MainWindow : Window
{
// Size of each square in pixels - kept constant for consistent display
private const int SQUARE_SIZE = 40;
// Grid dimensions - now variables instead of constants so they can change
// These will be updated based on user input from the textboxes
private int gridRows = 10;
private int gridCols = 10;
// 2D array to store all squares in the grid
// We use a 2D array because it naturally represents rows and columns
// This makes it easy to access a square at position [row, col]
private Square[,] grid;
// Boolean flag to control whether grid lines are drawn
// This demonstrates how to store application state
private bool showGridLines = true;
// Random number generator - created once and reused
// Creating it as a field ensures we get different random numbers each time
// If we created new Random() inside a method called quickly, we'd get same numbers
private readonly Random random = new Random();
private readonly GridRenderer renderer;
/// <summary>
/// Initialises the WPF window, creates the renderer, and draws the initial grid.
/// </summary>
public MainWindow()
{
InitializeComponent(); // built in method for WPF initialisation
renderer = new GridRenderer(DrawingCanvas);
InitialiseGrid(); // Set up the grid data structure
Redraw(); // Render the grid on the canvas
UpdateSelectedCount(); // Initialise the counter display
}
/// <summary>
/// Triggers a full redraw of the current grid state.
/// </summary>
private void Redraw()
{
renderer.DrawGrid(grid, showGridLines);
}
/// <summary>
/// (Re)creates the <see cref="Square"/> grid using the current <c>gridRows</c>/<c>gridCols</c>.
/// Each square is created with random walls.
/// </summary>
private void InitialiseGrid()
{
grid = new Square[gridRows, gridCols];
for (int row = 0; row < gridRows; row++)
{
for (int col = 0; col < gridCols; col++)
{
int x = col * SQUARE_SIZE;
int y = row * SQUARE_SIZE;
grid[row, col] = new Square(x, y, SQUARE_SIZE);
grid[row, col].RandomiseWalls(random);
}
}
}
/// <summary>
/// Handles left-mouse clicks on the canvas by toggling the selected state of the clicked square.
/// </summary>
private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Point clickPoint = e.GetPosition(DrawingCanvas);
for (int row = 0; row < gridRows; row++)
{
for (int col = 0; col < gridCols; col++)
{
Square square = grid[row, col];
if (square.ContainsPoint(clickPoint.X, clickPoint.Y))
{
square.IsSelected = !square.IsSelected;
Redraw();
UpdateSelectedCount();
return;
}
}
}
}
/// <summary>
/// Counts selected squares and updates the UI label to match.
/// </summary>
private void UpdateSelectedCount()
{
int count = 0;
for (int row = 0; row < gridRows; row++)
{
for (int col = 0; col < gridCols; col++)
{
if (grid[row, col].IsSelected)
{
count++;
}
}
}
SelectedCountLabel.Text = count.ToString();
}
/// <summary>
/// Validates the width/height textboxes, rebuilds the grid using those dimensions, and redraws.
/// </summary>
private void ApplyGridSize_Click(object sender, RoutedEventArgs e)
{
if (int.TryParse(WidthTextBox.Text, out int newWidth) &&
int.TryParse(HeightTextBox.Text, out int newHeight))
{
if (newWidth >= 1 && newWidth <= 50 && newHeight >= 1 && newHeight <= 50)
{
gridCols = newWidth;
gridRows = newHeight;
InitialiseGrid();
Redraw();
UpdateSelectedCount();
}
else
{
MessageBox.Show(
"Please enter grid dimensions between 1 and 50.",
"Invalid Input",
MessageBoxButton.OK,
MessageBoxImage.Warning
);
}
}
else
{
MessageBox.Show(
"Please enter valid numbers for width and height.",
"Invalid Input",
MessageBoxButton.OK,
MessageBoxImage.Warning
);
}
}
/// <summary>
/// Randomizes walls for every square in the grid and redraws.
/// </summary>
private void RandomiseWalls_Click(object sender, RoutedEventArgs e)
{
for (int row = 0; row < gridRows; row++)
{
for (int col = 0; col < gridCols; col++)
{
grid[row, col].RandomiseWalls(random);
}
}
Redraw();
}
/// <summary>
/// Clears selection on all squares and updates the UI.
/// </summary>
private void ClearSelection_Click(object sender, RoutedEventArgs e)
{
for (int row = 0; row < gridRows; row++)
{
for (int col = 0; col < gridCols; col++)
{
grid[row, col].IsSelected = false;
}
}
Redraw();
UpdateSelectedCount();
}
/// <summary>
/// Rebuilds the grid from scratch (new squares and new random walls) and updates the UI.
/// </summary>
private void ResetGrid_Click(object sender, RoutedEventArgs e)
{
InitialiseGrid();
Redraw();
UpdateSelectedCount();
}
/// <summary>
/// Selects all squares and updates the UI.
/// </summary>
private void SelectAll_Click(object sender, RoutedEventArgs e)
{
for (int row = 0; row < gridRows; row++)
{
for (int col = 0; col < gridCols; col++)
{
grid[row, col].IsSelected = true;
}
}
Redraw();
UpdateSelectedCount();
}
/// <summary>
/// Toggles visibility of grid/wall lines and redraws.
/// </summary>
private void ToggleGridLines_Click(object sender, RoutedEventArgs e)
{
showGridLines = !showGridLines;
Redraw();
}
/// <summary>
/// Exits the application by closing the main window.
/// </summary>
private void Exit_Click(object sender, RoutedEventArgs e)
{
Close();
}
/// <summary>
/// Shows a dialog describing what the project demonstrates.
/// </summary>
private void About_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(
"Grid Selection Example\n\n" +
"Demonstrates:\n" +
"- Drawing shapes with individual walls\n" +
"- 2D arrays for grid structure\n" +
"- Dynamic grid sizing\n" +
"- Mouse click event handling\n" +
"- Random wall generation\n" +
"- Input validation\n\n" +
"Perfect foundation for maze projects!",
"About",
MessageBoxButton.OK,
MessageBoxImage.Information
);
}
}
}