-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.js
697 lines (654 loc) · 25.7 KB
/
main.js
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
(function() {
var rectangleTopologyButton = window.document.getElementById("rectangleTopologyButton");
var outdoorTopologyButton = window.document.getElementById("outdoorTopologyButton");
var cylinderTopologyButton = window.document.getElementById("cylinderTopologyButton");
var torusTopologyButton = window.document.getElementById("torusTopologyButton");
var mobiusTopologyButton = window.document.getElementById("mobiusTopologyButton");
var sizeXTextbox = window.document.getElementById("sizeXTextbox");
var sizeYTextbox = window.document.getElementById("sizeYTextbox");
var depthFirstSearchAlgorithmButton = window.document.getElementById("depthFirstSearchAlgorithmButton");
var primAlgorithmButton = window.document.getElementById("primAlgorithmButton");
var kruskalAlgorithmButton = window.document.getElementById("kruskalAlgorithmButton");
var ivyAlgorithmButton = window.document.getElementById("ivyAlgorithmButton");
var depthFirstIvyAlgorithmButton = window.document.getElementById("depthFirstIvyAlgorithmButton");
var mazeCanvas = window.document.getElementById("mazeCanvas");
var mazeSerializationTextbox = window.document.getElementById("mazeSerializationTextbox");
var generationGoButton = window.document.getElementById("generationGoButton");
var generationStepButton = window.document.getElementById("generationStepButton");
var generationBeDoneButton = window.document.getElementById("generationBeDoneButton");
var generationInteractiveButton = window.document.getElementById("generationInteractiveButton");
var generationResetButton = window.document.getElementById("generationResetButton");
var longestPathGoButton = window.document.getElementById("longestPathGoButton");
var longestPathStepButton = window.document.getElementById("longestPathStepButton");
var longestPathBeDoneButton = window.document.getElementById("longestPathBeDoneButton");
var longestPathResetButton = window.document.getElementById("longestPathResetButton");
var shaveButton = window.document.getElementById("shaveButton");
var caveInButton = window.document.getElementById("caveInButton");
var resetExperimentsButton = window.document.getElementById("resetExperimentsButton");
var doorsPerRoomCheckbox = window.document.getElementById("doorsPerRoomCheckbox");
var doorsPerRoomCanvas = window.document.getElementById("doorsPerRoomCanvas");
var doorsPerRoom = null;
var doorsPerRoomHighlightIndex = null;
var wallsPerVertexCheckbox = window.document.getElementById("wallsPerVertexCheckbox");
var wallsPerVertexCanvas = window.document.getElementById("wallsPerVertexCanvas");
var wallsPerVertex = null;
var wallsPerVertexHighlightIndex = null;
var previousTopology;
var generator;
var generatorOptions;
var previousAlgorithm;
var maze;
var mazeSerialization = "";
var mazeRenderer;
var animationInterval = null;
var wasDone = true;
var longestPathAnimationInterval = null;
var interactiveGeneration = false;
var mouseHotspotsHighlightMaze = null;
var longestPathFinder;
var longestPathHighlightMaze;
var pathFinderPoints = [];
var pathHighlightMaze = null;
var doorsPerRoomHighlightMaze = null;
var wallsPerVertexHighlightMaze = null;
var experimentalMode = null;
specifySerialization(getSerializationFromUrl());
if (mazeSerialization === "") {
// the url serialization was not present or didn't work.
// start from scratch.
initGenerator();
}
function initGenerator(refresh) {
stopAnimation();
var topology = (function() {
switch (true) {
case rectangleTopologyButton.checked: return Maze.TOPOLOGY_RECTANGLE;
case outdoorTopologyButton.checked: return Maze.TOPOLOGY_OUTDOOR;
case cylinderTopologyButton.checked: return Maze.TOPOLOGY_CYLINDER;
case torusTopologyButton.checked: return Maze.TOPOLOGY_TORUS;
case mobiusTopologyButton.checked: return Maze.TOPOLOGY_MOBIUS;
}
throw new Error();
})();
var sizeX = parseInt(sizeXTextbox.value, 10) || 1;
var sizeY = parseInt(sizeYTextbox.value, 10) || 1;
var algorithmFunction = getAlgorithmFromUi();
if (!refresh) {
// if nothing's changed, don't reset.
// this happens when using the arrow keys in the size boxes.
if (previousTopology === topology &&
previousAlgorithm === algorithmFunction &&
maze.sizeX === sizeX &&
maze.sizeY === sizeY) {
return;
}
}
previousAlgorithm = algorithmFunction;
generator = new algorithmFunction(topology, sizeX, sizeY);
generatorOptions = generator.getOptions();
var newMaze = generator.maze;
if (generatorOptions == null) {
// well that was easy. we're already done.
generatorIsDone();
}
setMaze(newMaze);
}
function getAlgorithmFromUi() {
switch (true) {
case depthFirstSearchAlgorithmButton.checked: return DepthFirstSearchGenerator;
case primAlgorithmButton.checked: return PrimGenerator;
case kruskalAlgorithmButton.checked: return KruskalGenerator;
case ivyAlgorithmButton.checked: return IvyGenerator;
case depthFirstIvyAlgorithmButton.checked: return DepthFirstIvyGenerator;
}
throw new Error();
}
function setMaze(newMaze) {
maze = newMaze;
window._debug_maze = newMaze;
previousTopology = maze.topology;
rectangleTopologyButton.checked = previousTopology === Maze.TOPOLOGY_RECTANGLE;
outdoorTopologyButton.checked = previousTopology === Maze.TOPOLOGY_OUTDOOR;
cylinderTopologyButton.checked = previousTopology === Maze.TOPOLOGY_CYLINDER;
torusTopologyButton.checked = previousTopology === Maze.TOPOLOGY_TORUS;
mobiusTopologyButton.checked = previousTopology === Maze.TOPOLOGY_MOBIUS;
sizeXTextbox.value = maze.sizeX.toString();
sizeYTextbox.value = maze.sizeY.toString();
longestPathFinder = null;
longestPathHighlightMaze = null;
pathHighlightMaze = null;
pathFinderPoints = [];
doorsPerRoomHighlightMaze = null;
wallsPerVertexHighlightMaze = null;
experimentalMode = null;
mazeRenderer = new MazeRenderer(mazeCanvas, maze.topology, maze.sizeX, maze.sizeY);
refreshDisplay();
}
function waitAndInitGenerator() {
setTimeout(initGenerator, 0);
}
rectangleTopologyButton.addEventListener("click", waitAndInitGenerator);
outdoorTopologyButton.addEventListener("click", waitAndInitGenerator);
cylinderTopologyButton.addEventListener("click", waitAndInitGenerator);
torusTopologyButton.addEventListener("click", waitAndInitGenerator);
mobiusTopologyButton.addEventListener("click", waitAndInitGenerator);
sizeXTextbox.addEventListener("keydown", waitAndInitGenerator);
sizeYTextbox.addEventListener("keydown", waitAndInitGenerator);
depthFirstSearchAlgorithmButton.addEventListener("click", waitAndInitGenerator);
primAlgorithmButton.addEventListener("click", waitAndInitGenerator);
kruskalAlgorithmButton.addEventListener("click", waitAndInitGenerator);
ivyAlgorithmButton.addEventListener("click", waitAndInitGenerator);
depthFirstIvyAlgorithmButton.addEventListener("click", waitAndInitGenerator);
var heldDownMouseButton = null;
var scrollDragAnchorX;
var scrollDragAnchorY;
mazeCanvas.addEventListener("mousedown", function(event) {
if (event.shiftKey || event.ctrlKey || event.altKey) return;
heldDownMouseButton = event.button;
event.preventDefault();
if (heldDownMouseButton === 0) {
// left click
if (generator == null) {
// path finding on a done maze
var room = getRoomFromMouseEvent(event);
if (room == null) return;
// double click to clear
if (pathFinderPoints[0] === room) {
pathFinderPoints = [];
} else {
// this is the start point
pathFinderPoints = [room];
}
renderPath();
} else if (interactiveGeneration) {
// interactive maze generation
var value = (function() {
switch (generatorOptions.type) {
case "room":
return getRoomFromMouseEvent(event);
// TODO
// case "edge":
// case "branch":
// case "vector":
}
throw new Error();
})();
var index = generatorOptions.values.indexOf(value);
if (index !== -1) {
stepGenerator(index);
refreshDisplay();
}
}
} else {
// right or middle click
scrollDragAnchorX = eventToMouseX(event, mazeCanvas);
scrollDragAnchorY = eventToMouseY(event, mazeCanvas);
}
});
function eventToMouseX(event, canvas) { return event.clientX - canvas.getBoundingClientRect().left; }
function eventToMouseY(event, canvas) { return event.clientY - canvas.getBoundingClientRect().top; }
// why on the window instead of the canvas? see http://stackoverflow.com/questions/5418740/jquery-mouseup-outside-window-possible/5419564#5419564
window.addEventListener("mouseup", function() {
heldDownMouseButton = null;
});
mazeCanvas.addEventListener("mousemove", function(event) {
if (heldDownMouseButton === 0) {
// left-click drag
// this only works on a done maze
if (generator != null) return;
var room = getRoomFromMouseEvent(event);
if (room == null) return;
if (pathFinderPoints.length === 0) {
// dragged in from out of bounds or dragging after double-click to clear.
return;
}
if (pathFinderPoints[pathFinderPoints.length - 1] === room) {
// dragging around, but hasn't left the inital room yet.
return;
}
// this is the end point
pathFinderPoints[1] = room;
renderPath();
} else if (heldDownMouseButton === 2) {
// right- or middle-click drag
var x = eventToMouseX(event, mazeCanvas);
var y = eventToMouseY(event, mazeCanvas);
var deltaX = x - scrollDragAnchorX;
var deltaY = y - scrollDragAnchorY;
scrollDragAnchorX = x;
scrollDragAnchorY = y;
mazeRenderer.scroll(deltaX, deltaY);
refreshDisplay();
}
});
function getRoomFromMouseEvent(event) {
var roomLocation = mazeRenderer.getRoomLocationFromPixelLocation(eventToMouseX(event, mazeCanvas), eventToMouseY(event, mazeCanvas));
if (roomLocation == null) return null;
return maze.getRoomFromLocation(roomLocation.x, roomLocation.y);
}
mazeCanvas.addEventListener("contextmenu", function(event) {
if (event.shiftKey || event.ctrlKey || event.altKey) return;
event.preventDefault();
});
var PATH_HILIGHT = "#ffaaaa";
function renderPath() {
if (pathFinderPoints.length === 0) {
pathHighlightMaze = null;
} else {
pathHighlightMaze = new Maze(maze.topology, maze.sizeX, maze.sizeY);
if (pathFinderPoints.length === 1) {
pathHighlightMaze.roomColors[pathFinderPoints[0]] = PATH_HILIGHT;
} else {
var path = dijkstraSearch(maze, pathFinderPoints[0], pathFinderPoints[1]);
if (path != null) {
while (true) {
var room = path.pop();
pathHighlightMaze.roomColors[room] = PATH_HILIGHT;
if (path.length === 0) break;
var edge = path.pop();
pathHighlightMaze.edgeColors[edge] = PATH_HILIGHT;
}
}
}
}
refreshDisplay();
}
generationStepButton.addEventListener("click", function() {
stepGenerator();
refreshDisplay();
});
function getAnimationSpeed() {
return 1 + Math.floor(1000 / Math.max(maze.sizeX, maze.sizeY));
}
generationGoButton.addEventListener("click", function() {
if (generator == null) initGenerator(true);
if (animationInterval == null) {
// go
var animationSpeed = getAnimationSpeed();
animationInterval = setInterval(function() {
stepGenerator();
refreshDisplay();
}, animationSpeed);
generationGoButton.textContent = "Stop";
} else {
stopAnimation();
}
});
function stopAnimation() {
if (animationInterval == null) return;
clearInterval(animationInterval);
animationInterval = null;
generationGoButton.textContent = "Go";
}
generationBeDoneButton.addEventListener("click", function() {
stopAnimation();
if (generator == null) initGenerator(true);
while (generator != null) {
stepGenerator();
}
refreshDisplay();
});
generationInteractiveButton.addEventListener("click", function() {
setTimeout(function() {
interactiveGeneration = !!generationInteractiveButton.checked;
refreshDisplay();
}, 0);
});
generationResetButton.addEventListener("click", function() {
initGenerator(true);
});
mazeSerializationTextbox.addEventListener("keydown", function() {
setTimeout(function() {
specifySerialization(mazeSerializationTextbox.value);
}, 0);
});
longestPathGoButton.addEventListener("click", function() {
if (longestPathAnimationInterval == null) {
// go
var animationSpeed = getAnimationSpeed();
longestPathAnimationInterval = setInterval(function() {
longestPathStep();
refreshDisplay();
}, animationSpeed);
longestPathGoButton.textContent = "Stop";
} else {
longestPathStopAnimation();
}
});
longestPathStepButton.addEventListener("click", function() {
longestPathStep();
refreshDisplay();
});
longestPathBeDoneButton.addEventListener("click", function() {
longestPathStep();
while (longestPathFinder != null) {
longestPathStep();
}
refreshDisplay();
});
longestPathResetButton.addEventListener("click", function() {
longestPathStopAnimation();
longestPathFinder = null;
longestPathHighlightMaze = null;
refreshDisplay();
});
function longestPathStopAnimation() {
if (longestPathAnimationInterval == null) return;
clearInterval(longestPathAnimationInterval);
longestPathAnimationInterval = null;
longestPathGoButton.textContent = "Go";
}
function longestPathStep() {
if (longestPathFinder == null) {
longestPathFinder = new LongestPathFinder(maze);
longestPathHighlightMaze = longestPathFinder.roomHighlightMaze;
} else {
longestPathFinder.step();
}
var endPoints = longestPathFinder.getEndPoints();
if (endPoints === LongestPathFinder.NOT_DONE_YET) return;
// done
longestPathStopAnimation();
longestPathFinder = null;
if (endPoints === LongestPathFinder.IMPOSSIBLE) return;
longestPathHighlightMaze = new Maze(maze.topology, maze.sizeX, maze.sizeY);
longestPathHighlightMaze.roomColors[endPoints[0]] = "#ff4444";
longestPathHighlightMaze.roomColors[endPoints[1]] = "#ff4444";
}
shaveButton.addEventListener("click", function() {
experimentalMode = "shave";
maze.shave();
refreshDisplay();
});
caveInButton.addEventListener("click", function() {
experimentalMode = "caveIn";
maze.caveIn();
refreshDisplay();
});
resetExperimentsButton.addEventListener("click", function() {
generator = null;
setMaze(Maze.fromSerialization(mazeSerialization));
});
doorsPerRoomCheckbox.addEventListener("click", function() {
setTimeout(updateStatistics, 0);
});
doorsPerRoomCanvas.addEventListener("mousemove", function(event) {
doorsPerRoomHighlightIndex = getHistogramRowIndex(doorsPerRoom, eventToMouseY(event, doorsPerRoomCanvas));
updateDoorsPerRoomHighlightMaze();
renderMaze();
renderHistogram(doorsPerRoomCanvas, doorsPerRoom, doorsPerRoomHighlightIndex);
});
doorsPerRoomCanvas.addEventListener("mouseout", function() {
doorsPerRoomHighlightMaze = null;
doorsPerRoomHighlightIndex = null;
renderMaze();
renderHistogram(doorsPerRoomCanvas, doorsPerRoom, doorsPerRoomHighlightIndex);
});
function updateDoorsPerRoomHighlightMaze() {
if (doorsPerRoomHighlightIndex != null) {
doorsPerRoomHighlightMaze = new Maze(maze.topology, maze.sizeX, maze.sizeY);
doorsPerRoom[doorsPerRoomHighlightIndex].values.forEach(function(i) {
doorsPerRoomHighlightMaze.roomColors[i] = "#ff4444";
});
} else {
doorsPerRoomHighlightMaze = null;
}
}
wallsPerVertexCheckbox.addEventListener("click", function() {
setTimeout(updateStatistics, 0);
});
wallsPerVertexCanvas.addEventListener("mousemove", function(event) {
wallsPerVertexHighlightIndex = getHistogramRowIndex(wallsPerVertex, eventToMouseY(event, wallsPerVertexCanvas));
updateWallsPerVertexHighlightMaze();
renderMaze();
renderHistogram(wallsPerVertexCanvas, wallsPerVertex, wallsPerVertexHighlightIndex);
});
wallsPerVertexCanvas.addEventListener("mouseout", function() {
wallsPerVertexHighlightMaze = null;
wallsPerVertexHighlightIndex = null;
renderMaze();
renderHistogram(wallsPerVertexCanvas, wallsPerVertex, wallsPerVertexHighlightIndex);
});
function updateWallsPerVertexHighlightMaze() {
if (wallsPerVertexHighlightIndex != null) {
wallsPerVertexHighlightMaze = new Maze(maze.topology, maze.sizeX, maze.sizeY);
wallsPerVertex[wallsPerVertexHighlightIndex].values.forEach(function(i) {
wallsPerVertexHighlightMaze.vertexColors[i] = "#ff4444";
});
} else {
wallsPerVertexHighlightMaze = null;
}
}
function stepGenerator(index) {
if (generatorOptions != null) {
// let's do something
if (index == null) {
// pick one at random
index = util.randomInt(generatorOptions.values.length);
}
generator.doOption(index);
}
generatorOptions = generator.getOptions();
if (generatorOptions == null) {
// and now we're done
generatorIsDone();
}
}
function generatorIsDone() {
generator = null;
stopAnimation();
}
function refreshDisplay() {
updateStatistics();
updateDoorsPerRoomHighlightMaze();
updateWallsPerVertexHighlightMaze();
updateMouseHotspotsHighlightMaze();
renderMaze();
}
function renderMaze() {
var context = mazeCanvas.getContext("2d");
context.clearRect(0, 0, mazeCanvas.width, mazeCanvas.height);
if (longestPathHighlightMaze != null) {
mazeRenderer.render(longestPathHighlightMaze);
}
if (pathHighlightMaze != null) {
mazeRenderer.render(pathHighlightMaze);
}
mazeRenderer.render(maze);
if (doorsPerRoomHighlightMaze != null) {
mazeRenderer.render(doorsPerRoomHighlightMaze, {roomSpacing:mazeRenderer.cellSize / 2});
}
if (wallsPerVertexHighlightMaze != null) {
mazeRenderer.render(wallsPerVertexHighlightMaze, {vertexThickness:mazeRenderer.cellSize / 2});
}
if (mouseHotspotsHighlightMaze != null) {
mazeRenderer.render(mouseHotspotsHighlightMaze, {
// enlarge whatever's being rendered
roomSpacing: mazeRenderer.cellSize / 2,
edgeThickness: mazeRenderer.cellSize / 3,
vertexThickness: mazeRenderer.cellSize / 2,
});
}
var nowDone = generator == null;
if (nowDone !== wasDone) {
setEnabled(generationStepButton, !nowDone);
mazeSerialization = nowDone ? maze.getSerialization() : "";
mazeSerializationTextbox.value = mazeSerialization;
}
if (!nowDone) {
setEnabled(longestPathGoButton, false);
setEnabled(longestPathStepButton, false);
setEnabled(longestPathBeDoneButton, false);
setEnabled(longestPathResetButton, false);
setEnabled(shaveButton, false);
setEnabled(caveInButton, false);
setEnabled(resetExperimentsButton, false);
} else {
setEnabled(longestPathGoButton, experimentalMode == null);
setEnabled(longestPathStepButton, experimentalMode == null);
setEnabled(longestPathBeDoneButton, experimentalMode == null);
setEnabled(longestPathResetButton, longestPathHighlightMaze != null);
setEnabled(shaveButton, experimentalMode == null || experimentalMode === "shave");
setEnabled(caveInButton, experimentalMode == null || experimentalMode === "caveIn");
setEnabled(resetExperimentsButton, experimentalMode != null);
}
wasDone = nowDone;
}
function updateMouseHotspotsHighlightMaze() {
if (generator == null || !interactiveGeneration) {
mouseHotspotsHighlightMaze = null;
return;
}
mouseHotspotsHighlightMaze = new Maze(maze.topology, maze.sizeX, maze.sizeY);
var CLICK_ME_COLOR = "#dddd00";
generatorOptions.values.forEach(function(value) {
switch (generatorOptions.type) {
case "room":
mouseHotspotsHighlightMaze.roomColors[value] = CLICK_ME_COLOR;
return;
case "edge":
mouseHotspotsHighlightMaze.edgeColors[value] = CLICK_ME_COLOR;
return;
// TODO
// case "branch":
// case "vector":
}
throw new Error();
});
}
function updateStatistics() {
doorsPerRoom = null;
if (doorsPerRoomCheckbox.checked) {
doorsPerRoom = [
{label: "0", values: []},
{label: "1", values: []},
{label: "2", values: []},
{label: "3", values: []},
{label: "4", values: []},
{label: "?", values: []},
];
var roomCount = maze.getRoomCount();
for (var i = 0; i < roomCount; i++) {
var doorCount = maze.roomToVectors(i).filter(function(vector) {
return maze.edgeColors[vector.edge] === Maze.OPEN;
}).length;
if (doorCount < 5) {
doorsPerRoom[doorCount].values.push(i);
} else {
// outdoor rooms can have an arbitrary number of rooms
// we'll need another for outdoor cylinders
doorsPerRoom[5].values.push(i);
doorsPerRoom[5].label = doorCount.toString();
}
}
if (doorsPerRoom[5].values.length === 0) {
// usually blank, so don't show it
doorsPerRoom.pop();
}
}
renderHistogram(doorsPerRoomCanvas, doorsPerRoom, doorsPerRoomHighlightIndex);
wallsPerVertex = null;
if (wallsPerVertexCheckbox.checked) {
wallsPerVertex = [
{label: "0", values: []},
{label: "1", values: []},
{label: "2", values: []},
{label: "3", values: []},
{label: "4", values: []},
];
var vertexCount = maze.getVertexCount();
for (var i = 0; i < vertexCount; i++) {
var wallCount = maze.vertexToEdges(i).filter(function(edge) {
return maze.edgeColors[edge] === Maze.FILLED;
}).length;
wallsPerVertex[wallCount].values.push(i);
}
}
renderHistogram(wallsPerVertexCanvas, wallsPerVertex, wallsPerVertexHighlightIndex);
}
var histogramFontHeight = 16;
// this is a guess. as of writing this,
// the api to get the height including low-hanging glyphs like "g" are not supported yet
// (except for ExperimentalCanvasFeatures in chrome).
var moreRealisticHistorgramTextHeight = histogramFontHeight * 1.2;
function renderHistogram(canvas, data, highlightRowIndex) {
if (data == null) {
canvas.width = 0;
canvas.height = 0;
return;
}
// figure out how big everything's going to be
var font = histogramFontHeight + "px sans-serif";
var measuringContext = canvas.getContext("2d");
measuringContext.font = font;
var maxLabelWidth = 0;
var longestPossibleBar = 0;
for (var i = 0; i < data.length; i++) {
var textMetrics = measuringContext.measureText(data[i].label);
// it's too bad this TextMetrics object doesn't have a .height property too.
maxLabelWidth = Math.max(maxLabelWidth, textMetrics.width);
longestPossibleBar += data[i].values.length;
}
var graphWidth = Math.max(300, maxLabelWidth + 200);
canvas.height = data.length * moreRealisticHistorgramTextHeight;
canvas.width = graphWidth;
// reget the context after resizing the canvas?
var context = canvas.getContext("2d");
context.fillStyle = "#ffffff";
context.fillRect(0, 0, canvas.width, canvas.height);
context.font = font;
context.textBaseline = "top";
context.textAlign = "right";
context.strokeStyle = "#000000";
for (var i = 0; i < data.length; i++) {
context.strokeText(data[i].label, maxLabelWidth, moreRealisticHistorgramTextHeight * i);
}
var maxBarLength = graphWidth - maxLabelWidth;
context.textAlign = "left";
for (var i = 0; i < data.length; i++) {
if (i === highlightRowIndex) {
context.fillStyle = "#ff8888";
} else {
context.fillStyle = "#8888ff";
}
var barWidth = maxBarLength * data[i].values.length / longestPossibleBar;
var x = maxLabelWidth;
var y = moreRealisticHistorgramTextHeight * i;
context.fillRect(x, y, barWidth, moreRealisticHistorgramTextHeight);
context.strokeText(data[i].values.length, x + 3, y);
}
}
function getHistogramRowIndex(histogramData, pixelY) {
// if we get mouse events for a hidden histogram for some reason.
if (histogramData == null) return null;
var index = Math.floor(pixelY / moreRealisticHistorgramTextHeight);
// bounds check
if (!(0 <= index && index < histogramData.length)) return null;
return index;
}
function setEnabled(button, value) {
if (value) {
button.removeAttribute("disabled");
} else {
button.setAttribute("disabled", "");
}
}
function specifySerialization(newSerialization) {
if (mazeSerialization === newSerialization) return;
var candidateMaze = Maze.fromSerialization(newSerialization);
if (candidateMaze == null) return;
generatorIsDone();
// make sure we think this new maze is a cool new idea
wasDone = false;
setMaze(candidateMaze);
// The algorithm is not stored in the data, so read the algorithm from the ui.
// This prevents resetting the maze if you click on the already-selected default algorithm radio button
// after loading the page with a serialization data url.
previousAlgorithm = getAlgorithmFromUi();
}
function getSerializationFromUrl() {
var match = /#data=(.*)/.exec(location.hash);
if (match == null) return "";
return match[1];
}
})();