-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractions.js
207 lines (185 loc) · 5.31 KB
/
interactions.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
// INTERACTIONS
const rotPerDrag = 1/800;
const truckperDrag = 1/1000;
var firstClickPos = null;
var drag = false;
var down = false;
var button = null;
// CLICK AND DRAG ON CANVAS EVENTS
function getCubeFace(event, canvas) {
// Get click coordinates
var correction = canvas.getBoundingClientRect();
var t = vec2(event.clientX-correction.left,
canvas.height-(event.clientY-correction.top));
// Get color in look up map
var lookUpColor = new Uint8Array(4);
gl.bindFramebuffer(gl.FRAMEBUFFER, lookUpMapFbo);
gl.readPixels(t[0], t[1], 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, lookUpColor);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
// Get cube and face indices from color in look up map
var cubeIndex = null;
var faceIndex = null;
if(lookUpColor[3] !== 0){
cubeIndex = lookUpColor[0];
faceIndex = lookUpColor[1] * 5 / 255;
}
return {cubeIndex: cubeIndex, faceIndex: faceIndex};
}
function clickOnCanvas(event) {
let clickedFace = getCubeFace(event, canvas);
if (clickedFace.cubeIndex !== null) {
if (modes.draw) {
if (modes.add) {
allCubes.push(createNewCube(clickedFace.cubeIndex, clickedFace.faceIndex, drawingColor));
}
else {
if (allCubes.length > 1) removeCube(clickedFace.cubeIndex);
}
drawScene();
}
else {
// Color pick
let color = allCubes[clickedFace.cubeIndex].color;
drawingColor = vec4(color[0],
color[1],
color[2],
color[2]);
}
}
// console.log("cubeIndex = " + cubeIndex + ", faceIndex = " + faceIndex);
}
function hoverCanvas(event, canvas) {
let hoveredFace = getCubeFace(event, canvas);
if (hoveredFace.cubeIndex !== null) {
let newPredictedCube;
if (modes.draw) {
if ( modes.add){
newPredictedCube = createNewCube(hoveredFace.cubeIndex, hoveredFace.faceIndex, drawingColor);
}
else {
newPredictedCube = {
vertices: allCubes[hoveredFace.cubeIndex].vertices,
color: vec4(1, 0, 0, 1)
}
}
}
else {
// Color picker mode
newPredictedCube = {
vertices: allCubes[hoveredFace.cubeIndex].vertices,
color: vec4(1, 1, 1, 1)
}
}
if (!predictedCube[0] || !predictedCube[0].vertices.equals(newPredictedCube.vertices)) {
predictedCube = [newPredictedCube];
drawCubes(allCubes, 0); // Doesn't work without redrawing the main scene first
drawCubes(predictedCube, 3);
drawLight();
}
}
else {
if (predictedCube.length > 0) {
drawCubes(allCubes, 0); // Necessary to remove the previously predicted cube wireframe
drawLight();
predictedCube = [];
}
}
}
function mousedown(e, canvas) {
// Get click coordinates
var correction = canvas.getBoundingClientRect();
firstClickPos = vec2(e.clientX-correction.left,
canvas.height-(e.clientY-correction.top));
drag = false; // Reinitialize drag flag
down = true;
button = e.button;
}
function mousemove(event, canvas) {
if (down) {
// Get click coordinates
var correction = canvas.getBoundingClientRect();
var secondClickPos = vec2(event.clientX-correction.left,
canvas.height-(event.clientY-correction.top));
if (firstClickPos) {
var dist = subtract(firstClickPos, secondClickPos);
// console.log("deltaX= " + dist[0] + " deltaY= " + dist[1]);
if (Math.pow(dist[0], 2) + Math.pow(dist[1], 2) > 10) {
// It was a drag
drag = true;
if (button == 0) {
var deltaPhi = rotPerDrag * dist[0];
var deltaTheta = - rotPerDrag * dist[1];
rotate(deltaTheta, deltaPhi);
}
if (button == 2) {
truck(dist[0] * truckperDrag, dist[1] * truckperDrag);
}
}
}
}
else {
hoverCanvas(event, canvas);
}
}
function mouseup(event, canvas) {
// Check if drag or click
if (drag) {
// Stop dragging
firstClickPos = null;
}
else {
// It was a click
firstClickPos = null;
clickOnCanvas(event);
}
down = false;
drag = false;
button = null;
// Draw current look up map
// Drawing look up map only once the mouse movement is finished leads to fewer calls to gl.drawArrays
drawCubes(allCubes, 1);
}
function zoomWheel(event) {
event.preventDefault(); // Prevent scrolling
if (event.deltaY < 0) {
zoom(0.5);
}
if (event.deltaY > 0) {
zoom(-0.5);
}
}
function toggleMode(button, mode) {
if (button.classList.contains("active")) return; // If already selected, do nothing
button.classList.add("active");
let otherButton = button.nextElementSibling || button.previousElementSibling;
otherButton.classList.remove("active");
modes[mode] = !modes[mode];
drawScene();
}
function truck(dx, dy) {
x += dx;
y += dy;
moveCamera(r, theta, phi, x, y);
// Draw current look up map
drawCubes(allCubes, 1);
drawScene();
}
function zoom(factor) {
if (r - factor > 3 && r - factor < 25) {
r -= factor;
moveCamera(r, theta, phi, x, y);
// Draw current look up map
drawCubes(allCubes, 1);
drawScene();
}
}
function rotate(dtheta, dphi) {
phi += dphi;
if (theta + dtheta > 0 && theta + dtheta < 3.1) {
theta += dtheta;
}
moveCamera(r, theta, phi, x, y);
// Draw current look up map
drawCubes(allCubes, 1);
drawScene();
}