-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.cs
More file actions
216 lines (179 loc) · 6.76 KB
/
Node.cs
File metadata and controls
216 lines (179 loc) · 6.76 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
namespace PathfindingAlgorithmVisualiser
{
public class Node
{
public NodeType Type { get; private set; }
public NodeType PreviousNodeType { get; set; }
public List<Node> Neighbours { get; set; }
public Node Parent { get; set; }
public int Column { get; private set; }
public int Row { get; private set; }
public int AdjacentCost { get; private set; }
public int GCost { get; set; }
private readonly Label _lbl = new();
public Node(int size, int x, int y, int col, int row, Form form)
{
Type = NodeType.Empty;
Neighbours = new List<Node>();
AdjacentCost = 1;
Column = col;
Row = row;
Parent = this;
CreateLbl(size, x, y, form);
}
public void Open()
{
_lbl.BackColor = Color.GreenYellow;
}
public void Close()
{
_lbl.BackColor = Color.Yellow;
}
public void Path()
{
_lbl.BackColor = Color.Violet;
}
public void Dim()
{
// Makes label semi-transparent.
_lbl.BackColor = Color.FromArgb(50, _lbl.BackColor);
}
public void Reset()
{
Type = NodeType.Empty;
AdjacentCost = 1;
_lbl.BackColor = Color.WhiteSmoke;
}
public void MakeStart()
{
if (Grid.Start is not null)
{
if (Grid.Moving is not null && Grid.Start.PreviousNodeType == NodeType.Block)
Grid.Start.MakeBlock();
else if (Grid.Moving is not null && Grid.Start.PreviousNodeType == NodeType.Dense)
Grid.Start.MakeDense();
else
Grid.Start.Reset();
}
PreviousNodeType = Type;
Type = NodeType.Start;
_lbl.BackColor = Color.Green;
Grid.Start = this;
}
public void MakeTarget()
{
if (Grid.Target is not null)
{
if (Grid.Moving is not null && Grid.Target.PreviousNodeType == NodeType.Block)
Grid.Target.MakeBlock();
else if (Grid.Moving is not null && Grid.Target.PreviousNodeType == NodeType.Dense)
Grid.Target.MakeDense();
else
Grid.Target.Reset();
}
PreviousNodeType = Type;
Type = NodeType.Target;
_lbl.BackColor = Color.Red;
Grid.Target = this;
}
public void MakeBlock()
{
Type = NodeType.Block;
_lbl.BackColor = Color.Black;
}
public void MakeDense()
{
Type = NodeType.Dense;
AdjacentCost = 15;
_lbl.BackColor = Color.SaddleBrown;
}
private void MakeDiversion()
{
if (Grid.Diversion is not null)
{
if (Grid.Moving is not null && Grid.Diversion.PreviousNodeType == NodeType.Block)
Grid.Diversion.MakeBlock();
else if (Grid.Moving is not null && Grid.Diversion.PreviousNodeType == NodeType.Dense)
Grid.Diversion.MakeDense();
else
Grid.Diversion.Reset();
}
PreviousNodeType = Type;
Type = NodeType.Diversion;
_lbl.BackColor = Color.Blue;
Grid.Diversion = this;
}
private void CreateLbl(int size, int x, int y, Form form)
{
// Nodes should be square, so label height and width are the same.
_lbl.Size = new Size(size, size);
_lbl.Location = new Point(x, y);
_lbl.BorderStyle = BorderStyle.FixedSingle;
_lbl.BackColor = Color.WhiteSmoke;
// Binding label events and handlers.
_lbl.MouseDown += Lbl_MouseDown;
_lbl.MouseEnter += Lbl_MouseEnter;
_lbl.MouseUp += Lbl_MouseUp;
// Adds the label to the form so that it's visible to the user.
form.Controls.Add(_lbl);
}
private void Lbl_MouseUp(object? sender, MouseEventArgs e)
{
// User has stopped dragging.
Grid.Dragging = false;
Grid.ButtonHolding = null;
Grid.Moving = null;
}
private void Lbl_MouseEnter(object? sender, EventArgs e)
{
// The label will only be updated if the user is 'dragging' and not a 'core' node.
if (Grid.Dragging && Type != NodeType.Start && Type != NodeType.Target && Type != NodeType.Diversion)
{
if (Grid.Moving == NodeType.Start)
MakeStart();
else if (Grid.Moving == NodeType.Target)
MakeTarget();
else if (Grid.Moving == NodeType.Diversion)
MakeDiversion();
else
UpdateLbl(Grid.ButtonHolding);
if (Simulation.Visualised)
Simulation.InstantRecalculation();
}
}
private void Lbl_MouseDown(object? sender, MouseEventArgs e)
{
if (Simulation.Running && !Simulation.Visualised)
return;
// This makes it so that other labels can listen for 'MouseEnter' event while the mouse is still pressed.
if (sender is Control control)
control.Capture = false;
// Determines if the user is moving or adding a node.
if (Type == NodeType.Start || Type == NodeType.Target || Type == NodeType.Diversion)
{
if (e.Button == MouseButtons.Left)
Grid.Moving = Type;
}
else if (!Simulation.Running)
{
UpdateLbl(e.Button);
Grid.ButtonHolding = e.Button;
}
// Initiates the dragging process.
Grid.Dragging = true;
}
private void UpdateLbl(MouseButtons? button)
{
// Edits node based on which mouse button is pressed, and which node type is selected from the toolbar.
if (button == MouseButtons.Left)
{
if (Toolbar.SelectedNodeType == NodeType.Block)
MakeBlock();
else if (Toolbar.SelectedNodeType == NodeType.Dense)
MakeDense();
}
else if (button == MouseButtons.Right)
Reset();
}
}
}