-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIController.js
94 lines (81 loc) · 3.77 KB
/
UIController.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
// User Interface controller
var UIController = (function () {
var matrixPlace;
var dirButtons = document.querySelectorAll('#solve-dir button');
var btnStyles = ['wallState', 'pathState', 'startState', 'stopState', 'visitedState', 'wayState', 'actualState', 'semiWayState'];
return {
init: function () {
},
matrixCreate: function (width, height, btnSize) {
matrixPlace = document.getElementById('matrix-place');
this.matrixRemove(matrixPlace);
matrixPlace.style.width = width * btnSize + 'px';
matrixPlace.style.height = height * btnSize + 'px';
for (var y = 0; y < height; y++) {
for (var x = 0; x < width; x++) {
var btn = document.createElement("BUTTON");
btn.setAttribute("id", 'btn-' + x + '-' + y);
btn.setAttribute("class", 'matrixBtn');
matrixPlace.appendChild(btn);
btn.style.width = btnSize + 'px';
btn.style.height = btnSize + 'px';
btn.style.fontSize = 0.8 * btnSize + 'px';
// document.getElementById('btn-' + x + '-' + y).innerHTML = '';
}
}
},
matrixRemove: function (matrixPlace) {
// matrixPlace = document.getElementById('matrix-place');
for (var i = matrixPlace.childNodes.length; i > 0; i--) {
matrixPlace.removeChild(matrixPlace.childNodes[i - 1]);
}
},
btnState: function (x, y, btnState) {
var pressedBtn = document.getElementById('btn-' + x + '-' + y);
// just in case clear all styles - wallStyle is deafult
for (var state of btnStyles) {
pressedBtn.classList.remove(state);
}
if (typeof (btnState) !== undefined) pressedBtn.classList.add(btnStyles[btnState]);
},
toggleState: function (x, y, btnState, adding) {
var pressedBtn = document.getElementById('btn-' + x + '-' + y);
if (adding) {
pressedBtn.classList.add(btnState);
}
else {
pressedBtn.classList.remove(btnState);
}
},
setPlayButton: function (state) {
var runButton = document.getElementById('run-algorithm');
if (state === 'play') {
runButton.innerHTML = '<i class="far fa-play-circle"></i>Run algorithm';
runButton.classList.remove('button-pressed');
} else if (state === 'pause') {
runButton.innerHTML = '<i class="far fa-pause-circle"></i>Pause';
runButton.classList.remove('button-pressed');
}
else if (state === 'paused') {
runButton.classList.add('button-pressed');
}
},
setDirButton: function (pressedBtn) {
console.log(pressedBtn);
console.log(dirButtons);
dirButtons = Array.from(dirButtons);
for (button of dirButtons) {
button.classList.remove('button-pressed');
}
pressedBtn.classList.add('button-pressed');
},
updateResult: function (result) {
document.getElementById("algorithm-last").innerText = result.algorithm;
document.getElementById("visited-last").innerText = result.visited;
document.getElementById("path-last").innerText = result.path[0];
document.getElementById("algorithm-previous").innerText = result.previousAlgorithm;
document.getElementById("visited-previous").innerText = result.previousVisited;
document.getElementById("path-previous").innerText = result.previousPath[0];
}
}
})();