forked from bferdinandus/SwitchBoard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.pde
383 lines (330 loc) · 11 KB
/
Board.pde
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
/*
Een wissel ziet er als volgt uit.
Terminal a: waar de splitsing begint
Terminal b: het stuk dat rechtdoor loopt
Terminal c: het stuk dat afbuigt
C
/
/
A--B
*/
public class Board
{
private Map<Integer, Node> _nodes = new TreeMap<Integer, Node>();
private ArrayList<Button> _buttons = new ArrayList();
private Track _fromTrack, _toTrack;
private Boolean _displayRouteError = false;
private String _name = "";
public Board() {
_buttons.add(new Button(Constants.buttons.Reset, "Reset", 300, 5));
_buttons.add(new Button(Constants.buttons.PlanRoute, "Bereken!", 300, 30));
}
public String Name() {
return _name;
}
public void Name(String name) {
_name = name;
}
public Track FromTrack() {
return _fromTrack;
}
public Track ToTrack() {
return _toTrack;
}
public void FromTrack(Track track) {
_fromTrack = track;
}
public void ToTrack(Track track) {
_toTrack = track;
}
private void ResetTracks() {
_fromTrack = null;
_toTrack = null;
}
private void PlanRoute() {
ResetHighlights();
_displayRouteError = false;
Planner p = new Planner(this);
Boolean calculateSuccess = p.CalculateRoute(_fromTrack.Id(), _toTrack.Id());
if (calculateSuccess) {
p.ExecuteRoute();
} else {
_displayRouteError = true;
}
}
private void ResetHighlights() {
for (Node node : _nodes.values()) {
node.get("self").Highlight(false);
}
}
public void AddElement(Constants.element type, Integer id)
{
AddElement(type, id, new HashMap<String, Object>());
}
// arguments: element type:enum, element Id:Integer, options: Map
public void AddElement(Constants.element type, Integer id, Map<String, Object> options)
{
if (NodeExists(id)) {
println("Board$AddElement => node id: " + id + " already exists.");
return;
}
Element element = CreateElement(type, id, options);
if (element == null) {
println("Board$AddElement => Invalid type: " + type.toString() + " No element added.");
return;
}
if (_nodes.size() == 0) {
// alleen voor het eerste id element de positie "hard" instellen. De rest gaat relatief ten opzichte van dit element
Map<String, Integer> xy = new HashMap<String, Integer>();
xy.put("x", 50);
xy.put("y", height / 2);
element.XY(xy);
}
Node node = new Node();
node.put("self", element);
_nodes.put(id, node);
}
private Element CreateElement(Constants.element type, Integer id, Map<String, Object> options) {
Element element = null;
switch (type) {
case SwitchTrack:
element = new SwitchTrackBuilder().setId(id).buildWithOptions(options);
break;
case Track:
element = new TrackBuilder().setId(id).buildWithOptions(options);
break;
default:
// niks doen
}
return element;
}
public void ConnectTerminals(Integer id1, Constants.terminal terminal1, Integer id2, Constants.terminal terminal2)
{
if (!NodeExists(id1)) {
println("Board$ConnectTerminals => Node id: " + id1 + " does not exist.");
return;
}
if (!NodeExists(id2)) {
println("Board$ConnectTerminals => Node id: " + id2 + " does not exist.");
return;
}
if (GetNodeById(id1).containsKey(terminal1.toString())) {
println("Board$ConnectTerminals => Terminal: " + terminal1.toString() + " on node id: " + id1 + " already taken.");
return;
}
if (GetNodeById(id2).containsKey(terminal2.toString())) {
println("Board$ConnectTerminals => Connecting node id: " + id1 + " and node id: " + id2 + ". Terminal: " + terminal2.toString() + " on node id: " + id2 + " already taken.");
return;
}
// TODO: een type terminal check bouwen. Een recht stuk "Track" heeft bijvoorbeeld geen terminal C. Die zit alleen op een "SwitchTrack"
// TODO: juiste type voor element 1 en 2 bepalen
Element element1 = GetElementById(id1);
if (!element1.IsPositioned()) {
println("Board$ConnectTerminals => Element id: " + id1 + " not positioned. Unable to connect terminals.");
return;
}
Element element2 = GetElementById(id2);
GetNodeById(id1).put(terminal1.toString(), element2);
GetNodeById(id2).put(terminal2.toString(), element1);
Integer circleDiameter = Constants.circleDiameter;
if (!Constants.useNodeCircle) {
circleDiameter = 0;
}
// element 2 ten opzichte van element 1 positioneren.
Map<String, Integer> xy1 = element1.XY();
Map<String, Integer> xy2 = new HashMap<String, Integer>();
// bepalen of het 2e element in "reverse" moet op basis van de te verbinden terminals en de "reverse" van het 1e element
if (terminal1 == Constants.terminal.A && terminal2 == Constants.terminal.A && element1.Reverse() == element2.Reverse()) {
element2.Reverse(!element1.Reverse());
}
if (terminal1 == Constants.terminal.A
&& (terminal2 == Constants.terminal.B || terminal2 == Constants.terminal.B)
&& element1.Reverse() == !element2.Reverse()) {
element2.Reverse(element1.Reverse());
}
if ((terminal1 == Constants.terminal.B || terminal1 == Constants.terminal.B)
&& terminal2 == Constants.terminal.A
&& element1.Reverse() == !element2.Reverse()) {
element2.Reverse(element1.Reverse());
}
if ((terminal1 == Constants.terminal.B || terminal1 == Constants.terminal.C)
&& (terminal2 == Constants.terminal.B || terminal2 == Constants.terminal.C)
&& element1.Reverse() == element2.Reverse()) {
element2.Reverse(!element1.Reverse());
}
// x positie voor het 2e element bepalen
if (element1 instanceof Track) {
if (terminal1 == Constants.terminal.A) {
xy2.put("x", xy1.get("x") - ((Track) element1).Length() - circleDiameter);
}
if (terminal1 == Constants.terminal.B) {
xy2.put("x", xy1.get("x") + ((Track) element1).Length() + circleDiameter);
}
} else if (element1 instanceof SwitchTrack) {
if (element1.Reverse()
&& (terminal1 == Constants.terminal.B || terminal1 == Constants.terminal.C)) {
if (element2 instanceof SwitchTrack) {
xy2.put("x", xy1.get("x") - Constants.switchTrackWidth - circleDiameter);
} else if (element2 instanceof Track) {
xy2.put("x", xy1.get("x") - ((Track) element2).Length() - circleDiameter);
}
} else {
xy2.put("x", xy1.get("x") + Constants.switchTrackWidth + circleDiameter);
}
}
// y positie voor het 2e element bepalen
if (terminal1 == Constants.terminal.C) {
if (element2 instanceof SwitchTrack) {
if (element1.Flip()) {
xy2.put("y", xy1.get("y") + Constants.switchTrackHeight);
} else {
xy2.put("y", xy1.get("y") - Constants.switchTrackHeight);
}
} else if (element2 instanceof Track) {
if (element1.Flip()) {
xy2.put("y", xy1.get("y"));
} else {
xy2.put("y", xy1.get("y") - Constants.switchTrackHeight);
}
}
if (!element1.Flip() && element2.Flip()) {
// extra omhoog om rekening te houden met de flip
xy2.put("y", xy2.get("y") - Constants.switchTrackHeight);
}
if (element1.Flip() && !element2.Flip()) {
// extra omhoog om rekening te houden met de flip
xy2.put("y", xy2.get("y") + Constants.switchTrackHeight);
}
} else if (terminal2 == Constants.terminal.C) {
if (element2.Flip()) {
xy2.put("y", xy1.get("y") - Constants.switchTrackHeight);
} else {
if (element1 instanceof Track && ((Track) element1).Diagonal()) {
if (element1.Flip()) {
xy2.put("y", xy1.get("y") + Constants.switchTrackHeight + Constants.switchTrackHeight);
} else {
xy2.put("y", xy1.get("y"));
}
} else {
xy2.put("y", xy1.get("y") + Constants.switchTrackHeight);
}
}
} else {
xy2.put("y", xy1.get("y"));
}
// positie opslaan in 2e element
element2.XY(xy2);
}
public Element GetElementById(Integer id)
{
return GetNodeById(id).get("self");
}
public Map<Integer, Node> GetNodes()
{
return _nodes;
}
private Node GetNodeById(Integer id)
{
return _nodes.get(id);
}
private boolean NodeExists(Integer id) {
return _nodes.containsKey(id);
}
public void Display() {
DisplayText();
DisplayTracks();
DisplayButtons();
}
private void DisplayButtons()
{
for (Button button : _buttons) {
button.Display();
}
}
private void DisplayText() {
String fromTrackId = "", toTrackId = "";
if (_fromTrack != null) {
fromTrackId = _fromTrack.Id().toString();
}
if (_toTrack != null) {
toTrackId = _toTrack.Id().toString();
}
fill(#000000);
textAlign(LEFT, TOP);
textSize(15);
text("Automatisch route berekenen.\nStartspoor: " + fromTrackId + "\nEindspoor: " + toTrackId, 50, 5 );
if (_displayRouteError) {
fill(#FF0000);
textAlign(LEFT, TOP);
textSize(15);
text("Route niet mogelijk", 250, 70);
}
}
private void DisplayTracks() {
for (Node node : _nodes.values()) {
Element element = node.get("self");
if (element.IsPositioned()) {
element.Display();
} else {
//println("Draw => Element id: " + element.Id() + " not positioned: skip drawing");
}
}
}
public void MouseOverCheck(Integer x, Integer y) {
// check buttons
for (Button button : _buttons) {
button.MouseOverCheck(x, y);
}
// check board elements
for (Node node : _nodes.values()) {
Element element = node.get("self");
element.MouseOverCheck(x, y);
}
}
public void MouseClicked(Integer x, Integer y, Integer mButton) {
// check buttons
for (Button button : _buttons) {
if (button.MouseOverCheck(x, y) && mButton == LEFT && button.Id() == Constants.buttons.Reset) {
ResetTracks();
ResetHighlights();
_displayRouteError = false;
};
if (button.MouseOverCheck(x, y) && mButton == LEFT && button.Id() == Constants.buttons.PlanRoute) {
PlanRoute();
};
}
// check elements
for (Node node : _nodes.values()) {
Element element = node.get("self");
if (element.MouseOverCheck(mouseX, mouseY)) {
ResetHighlights();
_displayRouteError = false;
if (element instanceof SwitchTrack) {
((SwitchTrack) element).Toggle();
};
if (element instanceof Track) {
if (mouseButton == LEFT) {
_board.FromTrack((Track) element);
} else if (mouseButton == RIGHT) {
_board.ToTrack((Track) element);
}
}
}
}
}
public void MousePressed(Integer x, Integer y) {
// check buttons
for (Button button : _buttons) {
if (button.MouseOverCheck(x, y)) {
button.MousePressed();
};
}
}
public void MouseReleased(Integer x, Integer y) {
// check buttons
for (Button button : _buttons) {
button.MouseOverCheck(x, y);
button.MouseReleased();
}
}
}