-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouse.js
94 lines (93 loc) · 2.25 KB
/
mouse.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
let mouseIsPressed = false;
let movingDestination = false;
let mouseSetsWall = false; /*This decides if dragging set or removes the walls*/
let prevDestination = false;
function mousePressed() {
/*
* This is an event listener for mouse pressed
*/
if (onMenu()) {
return;
}
let box = grid.overbox();
if (source == null && box) {
//init start
console.log("initialising start");
box.path = true;
box.source = true;
source = box;
box.draw();
} else if (destination == null && box && box.source == false) {
//init destination
console.log("initialise destination");
box.path = true;
box.destination = true;
destination = box;
prevDestination = box;
box.draw();
} else if (box) {
if (box.destination) {
movingDestination = true;
mouseIsPressed = true;
} else if (box.source) {
console.log("moving source");
//code to move source here
} else {
mouseIsPressed = true;
box.path = !box.path;
mouseSetsWall = box.path;
box.draw();
}
}
}
function mouseDragged() {
/*
* Event listener for mouse dragged
*/
if (mouseIsPressed) {
let box = grid.overbox();
if (box) {
if (
movingDestination &&
box != destination &&
box.visitedFromSource &&
box != source
) {
console.log("moving");
prevDestination.destination = false;
destination = box;
box.destination = true;
prevDestination.draw();
prevDestination = box;
startpath();
box.draw();
} else if (box != destination && box != source) {
box.path = mouseSetsWall;
box.draw();
}
}
}
}
function mouseReleased() {
/*
* Event listener for mouse released
*/
mouseIsPressed = false;
movingDestination = false;
}
function onMenu() {
return false;
if (mouseX >= 5 && mouseX <= 45 && mouseY >= 5 && mouseY <= 27) return true;
if (menuOpen) {
if (mouseX >= 5 && mouseY >= 27)
//checking for menu when menu is expanded
return true;
}
return false;
}
function toggleMenuVisible() {
console.log("menu toggle");
var x = document.getElementById("menu");
if (x.style.display == "none") x.style.display = "block";
else x.style.display = "none";
}