-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimulationPanel.cs
255 lines (203 loc) · 6.6 KB
/
SimulationPanel.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
using System;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace LangontsAntSimulator
{
public partial class SimulationPanel : Panel
{
#region Private Member Declarations
private Color _actorColor;
private Bitmap _canvas;
private Size _previousSimulationSize;
private Point _scale;
private ISimulation _simulation;
private Color _taggedColor;
private Color _untaggedColor;
#endregion Private Member Declarations
#region Public Constructors
public SimulationPanel()
: this(null)
{ }
public SimulationPanel(IContainer container)
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
this.UpdateStyles();
if (container != null)
container.Add(this);
this.InitializeComponent();
this.TaggedColor = Color.Black;
this.UntaggedColor = Color.White;
this.ActorColor = Color.Red;
}
#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)
{
if (components != null)
components.Dispose();
this.Cleanup();
}
base.Dispose(disposing);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (this.Simulation != null)
{
if (this.Simulation.ShowOutput || this.Simulation.IsPaused)
{
if (_canvas != null)
e.Graphics.DrawImageUnscaled(_canvas, Point.Empty);
}
else
{
using (StringFormat format = new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center })
{
e.Graphics.DrawString("Output disabled. Please renable output to view the simulation.", this.Font, SystemBrushes.WindowText, this.ClientRectangle, format);
}
}
}
}
protected override void OnResize(EventArgs eventargs)
{
base.OnResize(eventargs);
this.RecreateCanvas();
}
#endregion Protected Overridden Methods
#region Public Methods
public void RedrawSimulation()
{
using (Graphics g = Graphics.FromImage(_canvas))
{
// draw the blocks
g.Clear(this.UntaggedColor);
foreach (IBlock block in _simulation.Blocks.Where(b => b.IsTagged))
this.DrawBlock(g, block);
// draw the actors
foreach (IActor actor in _simulation.Actors)
this.DrawActor(g, actor);
}
}
public void UpdateActors()
{
if (_previousSimulationSize != _simulation.Region.Size)
{
// simulation size has changed, need to recreate the whole tihng
this.RecreateCanvas();
}
else
{
// just redraw the actors
using (Graphics g = Graphics.FromImage(_canvas))
{
foreach (IActor actor in _simulation.Actors)
{
IBlock previousPosition;
IBlock position;
previousPosition = _simulation.Blocks.SingleOrDefault(b => b.Location == actor.PreviousLocation);
position = _simulation.Blocks.SingleOrDefault(b => b.Location == actor.Location);
if (previousPosition != null)
this.DrawBlock(g, previousPosition);
if (position != null)
this.DrawBlock(g, position);
this.DrawActor(g, actor);
}
}
this.Invalidate();
}
}
#endregion Public Methods
#region Public Properties
[DefaultValue(typeof(Color), "Red")]
public Color ActorColor
{
get { return _actorColor; }
set { _actorColor = value; }
}
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public ISimulation Simulation
{
get { return _simulation; }
set
{
_simulation = value;
this.RecreateCanvas();
this.Invalidate();
}
}
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Bitmap SimulationOutput
{ get { return _canvas; } }
[DefaultValue(typeof(Color), "Black")]
public Color TaggedColor
{
get { return _taggedColor; }
set { _taggedColor = value; }
}
[DefaultValue(typeof(Color), "White")]
public Color UntaggedColor
{
get { return _untaggedColor; }
set { _untaggedColor = value; }
}
#endregion Public Properties
#region Private Methods
private void Cleanup()
{
if (_canvas != null)
_canvas.Dispose();
}
private void DrawActor(Graphics g, IActor actor)
{
using (Brush brush = new SolidBrush(this.ActorColor))
g.FillEllipse(brush, this.GetBlockRegion(actor.Location));
}
private void DrawBlock(Graphics g, IBlock block)
{
Color color;
color = block.IsTagged ? this.TaggedColor : this.UntaggedColor;
using (Brush brush = new SolidBrush(color))
g.FillRectangle(brush, this.GetBlockRegion(block.Location));
}
private Rectangle GetBlockRegion(Point point)
{
int x;
int y;
// Maths is still not my strong point - hopefully this works
x = point.X + Math.Abs(_simulation.Region.X);
y = point.Y + Math.Abs(_simulation.Region.Y);
return new Rectangle(x * _scale.X, y * _scale.Y, _scale.X, _scale.Y);
}
public void RecreateCanvas()
{
if (!this.DesignMode && _simulation != null)
{
int scaleX;
int scaleY;
// store the size for future compares
_previousSimulationSize = _simulation.Region.Size;
// get the new scale
scaleX = _previousSimulationSize.Width > 0 && this.ClientSize.Width > 0 ? this.ClientSize.Width / _previousSimulationSize.Width : 1;
if (scaleX < 1)
scaleX = 1;
scaleY = _previousSimulationSize.Height > 0 && this.ClientSize.Height > 0 ? this.ClientSize.Height / _previousSimulationSize.Height : 1;
if (scaleY < 1)
scaleY = 1;
_scale = new Point(scaleX, scaleY);
// create the bitmap that will be used for drawing
this.Cleanup();
_canvas = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
this.RedrawSimulation();
}
}
#endregion Private Methods
}
}