-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
457 lines (374 loc) · 12.9 KB
/
MainForm.cs
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace LangontsAntSimulator
{
// http://blog.csharphelper.com/2011/02/26/contest-langtons-ant-simulation.aspx
public partial class MainForm : BaseForm, ISimulationHost
{
#region Private Member Declarations
private int _pauseOnMoveNumber;
private ISimulation _simulation;
private string _simulationFileName;
private readonly string _textPattern = "{2} - {0} [{1}]";
#endregion Private Member Declarations
#region Public Constructors
public MainForm()
{
InitializeComponent();
}
#endregion Public Constructors
#region Protected Overridden Methods
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
this.ClearSimulation();
if (components != null)
components.Dispose();
}
base.Dispose(disposing);
}
private void ClearSimulation()
{
if (_simulation != null)
_simulation.Stop(); // Clean up
}
#endregion Protected Overridden Methods
#region Event Handlers
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
using (AboutDialog dialog = new AboutDialog())
dialog.ShowDialog(this);
}
private void actorsComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (actorsComboBox.SelectedIndex >= 0 && actorsComboBox.SelectedIndex < actorsComboBox.Items.Count)
actorViewer.Actor = _simulation.Actors[actorsComboBox.SelectedIndex];
else
actorViewer.Actor = null;
}
private void addAntToolStripMenuItem_Click(object sender, EventArgs e)
{
using (AddAntsDialog dialog = new AddAntsDialog(_simulation))
{
if (dialog.ShowDialog(this) == DialogResult.OK)
this.InitializeActorsPane();
}
}
private void blackAndWhiteToolStripMenuItem_Click(object sender, EventArgs e)
{
this.SetColors(new ColorParameters()
{
ActorColor = Color.Red,
TaggedBlockColor = Color.Black,
UntaggedBlockColor = Color.White
});
}
private void coloursToolStripMenuItem_Click(object sender, EventArgs e)
{
using (ColorsDialog dialog = new ColorsDialog(new ColorParameters()
{
ActorColor = simulationPanel.ActorColor,
TaggedBlockColor = simulationPanel.TaggedColor,
UntaggedBlockColor = simulationPanel.UntaggedColor
}))
{
if (dialog.ShowDialog(this) == DialogResult.OK)
this.SetColors(dialog.GetColorParameters());
}
}
private void delayToolStripMenuItem_Click(object sender, EventArgs e)
{
using (DelayDialog dialog = new DelayDialog(_simulation))
dialog.ShowDialog(this);
}
private void drawOutputToolStripMenuItem_Click(object sender, EventArgs e)
{
_simulation.ShowOutput = !_simulation.ShowOutput;
this.UpdateOutputIndicators();
}
private void earthyToolStripMenuItem_Click(object sender, EventArgs e)
{
this.SetColors(new ColorParameters()
{
ActorColor = Color.Firebrick,
TaggedBlockColor = Color.DarkKhaki,
UntaggedBlockColor = Color.DarkOliveGreen
});
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void exportImageToolStripMenuItem_Click(object sender, EventArgs e)
{
bool isPausedRequired;
isPausedRequired = !_simulation.IsPaused;
if (isPausedRequired)
_simulation.Pause();
if (saveImageFileDialog.ShowDialog(this) == DialogResult.OK)
{
try
{
this.SetStatus("Exporting image...");
simulationPanel.SimulationOutput.Save(saveImageFileDialog.FileName, ImageFormat.Png);
MessageBox.Show("Image export complete.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Failed to export image. {0}", ex.Message), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally
{
this.SetStatus(string.Empty);
}
}
if (isPausedRequired)
_simulation.Start();
}
private void MainForm_Load(object sender, EventArgs e)
{
this.Font = SystemFonts.DialogFont;
this.InitializeSimulation(new SimulationStartParameters());
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
using (NewSimulationDialog dialog = new NewSimulationDialog())
{
if (dialog.ShowDialog(this) == DialogResult.OK)
this.InitializeSimulation(dialog.GetStartParameters());
}
}
private void nextMoveToolStripMenuItem_Click(object sender, EventArgs e)
{
_simulation.NextMove();
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
this.FileOpen(string.Empty);
}
private void pauseOnMoveToolStripMenuItem_Click(object sender, EventArgs e)
{
using (PauseOnMoveDialog dialog = new PauseOnMoveDialog() { MoveNumber = _pauseOnMoveNumber })
{
if (dialog.ShowDialog(this) == DialogResult.OK)
_pauseOnMoveNumber = dialog.MoveNumber;
}
}
private void pauseToolStripMenuItem_Click(object sender, EventArgs e)
{
if (_simulation.IsPaused)
_simulation.Start();
else
_simulation.Pause();
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
this.FileSave(string.Empty);
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
this.FileSave(_simulationFileName);
}
private void startToolStripMenuItem_Click(object sender, EventArgs e)
{
_simulation.Start();
}
private void stopToolStripMenuItem_Click(object sender, EventArgs e)
{
_simulation.Stop();
}
#endregion Event Handlers
#region Private Methods
private void FileOpen(string fileName)
{
if (string.IsNullOrEmpty(fileName))
{
openSimulationFileDialog.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
if (openSimulationFileDialog.ShowDialog(this) == DialogResult.OK)
fileName = openSimulationFileDialog.FileName;
}
if (!string.IsNullOrEmpty(fileName))
{
this.ClearSimulation();
_simulation = new LangtonsAntSimulation(this);
try
{
this.SetStatus("Loading simulation...");
_simulation.Load(fileName);
_simulationFileName = fileName;
this.InitializeSimulation(_simulation);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Failed to open simulation. {0}", ex.Message), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally
{
this.SetStatus(string.Empty);
}
}
}
private void FileSave(string fileName)
{
bool isPausedRequired;
isPausedRequired = !_simulation.IsPaused;
if (isPausedRequired)
_simulation.Pause();
if (string.IsNullOrEmpty(fileName))
{
saveSimulationFileDialog.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
saveSimulationFileDialog.FileName = _simulationFileName;
if (saveSimulationFileDialog.ShowDialog(this) == DialogResult.OK)
fileName = saveSimulationFileDialog.FileName;
}
if (!string.IsNullOrEmpty(fileName))
{
try
{
this.SetStatus("Saving simulation...");
_simulation.Save(fileName);
_simulationFileName = fileName;
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Failed to save simulation. {0}", ex.Message), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally
{
this.SetStatus(string.Empty);
}
}
if (isPausedRequired)
_simulation.Start();
}
private void InitializeActorsPane()
{
actorsComboBox.Items.Clear();
for (int i = 0; i < _simulation.Actors.Count(); i++)
actorsComboBox.Items.Add(string.Format("Ant {0}", i + 1));
if (actorsComboBox.Items.Count != 0)
actorsComboBox.SelectedIndex = 0;
}
private void InitializeSimulation(SimulationStartParameters simulationStartParameters)
{
List<IActor> actors;
_simulationFileName = string.Empty;
// destroy the old simulation or it will keep running with unintended consque
this.ClearSimulation();
_simulation = new LangtonsAntSimulation(this);
_simulation.Speed = simulationStartParameters.Delay;
actors = new List<IActor>();
for (int i = 0; i < simulationStartParameters.InitialActors; i++)
actors.Add(new Ant());
_simulation.Actors = actors.ToArray();
this.InitializeSimulation(_simulation);
_simulation.Start();
if (simulationStartParameters.StartPaused)
_simulation.Pause();
}
private void InitializeSimulation(ISimulation _simulation)
{
simulationPanel.Simulation = _simulation;
actorViewer.Simulation = _simulation;
this.InitializeActorsPane();
this.UpdateStatusPanel();
this.UpdateText(_simulation.IsPaused ? "Paused" : "Running");
}
private void SetColors(ColorParameters colorParameters)
{
simulationPanel.ActorColor = colorParameters.ActorColor;
simulationPanel.TaggedColor = colorParameters.TaggedBlockColor;
simulationPanel.UntaggedColor = colorParameters.UntaggedBlockColor;
simulationPanel.RedrawSimulation(); // need a full redraw if changing colors
simulationPanel.Invalidate();
}
private void SetStatus(string text)
{
statusToolStripStatusLabel.Text = text;
Application.DoEvents(); // force the status to appear
}
private void UpdateOutputIndicators()
{
drawOutputToolStripMenuItem.Checked = _simulation.ShowOutput;
simulationPanel.Invalidate(); // force a repaint
}
private void UpdateStatusPanel()
{
// show the simulation status
sizeToolStripStatusLabel.Text = string.Format("Arena: W:{0}, H:{1}", _simulation.Region.Width, _simulation.Region.Height);
moveToolStripStatusLabel.Text = string.Format("Move: {0}", _simulation.Move);
}
private void UpdateText(string action)
{
this.Text = string.Format(_textPattern, Application.ProductName, action, string.IsNullOrEmpty(_simulationFileName) ? "Untitled" : Path.GetFileName(_simulationFileName));
}
private void UpdateUi()
{
// menu items
startToolStripMenuItem.Enabled = !_simulation.IsRunning;
stopToolStripMenuItem.Enabled = _simulation.IsRunning;
pauseToolStripMenuItem.Enabled = _simulation.IsRunning;
pauseToolStripMenuItem.Checked = _simulation.IsPaused;
nextMoveToolStripMenuItem.Enabled = _simulation.IsPaused;
// toolbar buttons
startSimulationToolStripButton.Enabled = !_simulation.IsRunning;
stopSimulationToolStripButton.Enabled = _simulation.IsRunning;
pauseSimulationToolStripButton.Enabled = _simulation.IsRunning;
pauseSimulationToolStripButton.Checked = _simulation.IsPaused;
nextMoveToolStripButton.Enabled = _simulation.IsPaused;
}
#endregion Private Methods
#region ISimulationHost Members
void ISimulationHost.OnStart()
{
this.UpdateText("Running");
this.UpdateUi();
}
void ISimulationHost.OnStop()
{
this.UpdateText("Stopped");
this.UpdateUi();
}
void ISimulationHost.OnPause()
{
this.UpdateText("Paused");
this.UpdateUi();
}
void ISimulationHost.OnResume()
{
((ISimulationHost)this).OnStart();
}
void ISimulationHost.OnNextMove()
{
//System.Diagnostics.Debug.WriteLine("{0} Move {1}", DateTime.Now, _simulation.Move);
this.UpdateStatusPanel();
if (_simulation.ShowOutput || _simulation.IsPaused)
{
// redraw changed squares
simulationPanel.UpdateActors();
// show new information on the selected actor
actorViewer.UpdateUi();
}
// auto pause
if (_pauseOnMoveNumber > 0 && _simulation.Move == _pauseOnMoveNumber)
{
_simulation.Pause();
_simulation.ShowOutput = true;
this.UpdateOutputIndicators();
simulationPanel.RecreateCanvas();
MessageBox.Show(string.Format("Automatically paused due to reaching move {0}", _pauseOnMoveNumber), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
#endregion
}
}