-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathuserInput.js
181 lines (164 loc) · 3.86 KB
/
userInput.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
// User Input Stuff
// Introduce new body to system at mouse position
function initUI(canvasId) {
canvasElement = document.getElementById(canvasId);
//canvasElement.onclick = mouseClick;
canvasElement.onmousedown = mouseDown;
canvasElement.onmouseup = mouseUp;
canvasElement.onmousemove = mouseMove;
if (DEBUG) {
console.log("Initialize UI complete.");
}
setDT(-2);
}
function mouseClick(e) {
var mouseX, mouseY;
if (e.offsetX) {
mouseX = e.offsetX;
mouseY = e.offsetY;
} else if (e.layerX) {
mouseX = e.layerX;
mouseY = e.layerY;
} // IE
addBody(mouseX, mouseY, 0, 0);
}
// Drag stuff
var isDrag = false;
var dragx, dragy, dragm;
var dragx2, dragy2;
function mouseDown(e) {
isDrag = true;
var mouseX, mouseY;
if (e.offsetX) {
mouseX = e.offsetX;
mouseY = e.offsetY;
} else if (e.layerX) {
mouseX = e.layerX;
mouseY = e.layerY;
} // IE
dragx = mouseX;
dragy = mouseY;
dragx2 = mouseX;
dragy2 = mouseY;
refreshGraphics();
}
function mouseMove(e) {
if (isDrag) {
var mouseX, mouseY;
if (e.offsetX) {
mouseX = e.offsetX;
mouseY = e.offsetY;
} else if (e.layerX) {
mouseX = e.layerX;
mouseY = e.layerY;
} // IE
dragx2 = mouseX;
dragy2 = mouseY;
dragx2 = (mouseX - dragx) / arrowLengthRatio + dragx;
dragy2 = (mouseY - dragy) / arrowLengthRatio + dragy;
refreshGraphics();
}
}
function mouseUp(e) {
if (isDrag) {
isDrag = false;
var mouseX, mouseY;
if (e.offsetX) {
mouseX = e.offsetX;
mouseY = e.offsetY;
} else if (e.layerX) {
mouseX = e.layerX;
mouseY = e.layerY;
} // IE
mouseX = (mouseX - dragx) / arrowLengthRatio + dragx;
mouseY = (mouseY - dragy) / arrowLengthRatio + dragy;
addBody(dragx, dragy, mouseX - dragx, mouseY - dragy, dragm);
refreshGraphics();
}
}
// Update mass by arrow keys while dragging
window.addEventListener("keydown", doKeyDown, true);
var MASS_STEP = (MAXMASS - MINMASS) / 10;
dragm = (MINMASS + MAXMASS) / 2;
function doKeyDown(evt) {
switch (evt.keyCode) {
case 69 /* e was pressed */:
if (DEBUG) {
console.log("'e' key pressed");
}
if (isDrag && dragm + MASS_STEP <= MAXMASS) {
dragm += MASS_STEP;
}
break;
case 68 /* d key was pressed */:
if (DEBUG) {
console.log("'d' key pressed");
}
if (isDrag && dragm - MASS_STEP >= MINMASS) {
dragm -= MASS_STEP;
}
break;
case 80 /* p key was pressed */:
if (DEBUG) {
console.log("'p' key pressed");
}
if (sysRunning) {
pauseSys();
} else {
startSys();
}
break;
case 83 /* s key was pressed */:
if (DEBUG) {
console.log("'s' key pressed");
}
step();
break;
// case 37: /* Left arrow was pressed */
// if (x - dx > 0){
// x -= dx;
// }
// break;
// case 39: /* Right arrow was pressed */
// if (x + dx < WIDTH){
// x += dx;
// }
// break;
}
}
function setDT(v) {
dt = Math.pow(10, v);
if (DEBUG) {
console.log("DT SET: ", dt);
}
document.getElementById("dtSliderVal").innerHTML = dt.toFixed(4);
}
function setDEBUG(lvl) {
DEBUG = lvl % DEBUGMAX;
console.log("DEBUG SET: ", DEBUG);
document.getElementById("debugLVL").innerHTML = DEBUG;
document.getElementById("debugSlider").value = DEBUG;
refreshGraphics();
}
function toggleDEBUG() {
setDEBUG(DEBUG + 1);
}
function toggleArrows() {
drawArrows = !drawArrows;
console.log("SHOW ARROWS SET : ", drawArrows);
refreshGraphics();
}
function toggleShowBNtree() {
SHOW_BN_TREE = !SHOW_BN_TREE;
console.log("SHOW BN TREE : ", SHOW_BN_TREE);
if (!sysRunning) {
bnBuildTree();
}
refreshGraphics();
}
function resetSys() {
resetBodies();
bnDeleteTree();
console.log("DELETED ALL BODIES");
refreshGraphics();
}