-
Notifications
You must be signed in to change notification settings - Fork 4
/
scatterplot-ctrls.ts
440 lines (435 loc) · 17.3 KB
/
scatterplot-ctrls.ts
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
import * as AFRAME from 'aframe';
import { Scatterplot } from './scatterplot.d3';
import { rollup } from 'd3';
const ROBOTO = 'https://raw.githubusercontent.com/etiennepinchon/aframe-fonts/master/fonts/roboto/Roboto-Medium.json';
// define spatial placement of navigation tiles in a-scene
const tilePos: Record<string, string> = {
['xPos']: '-1.15 1 -4',
['xNeg']: '-2.15 1 -4',
['yPos']: '-1.65 1.5 -4',
['yNeg']: '-1.65 .5 -4',
['zPos']: '1.25 1.5 -4',
['zNeg']: '1.25 .5 -4',
};
// define spatial placement of speed tiles in a-scene
const speedPos: Record<string, string> = {
['minus']: '-.35 .5 -2',
['plus']: '.35 .5 -2',
['label']: '0 .45 -2',
};
// define spatial placement of x-scale tiles in a-scene
const xScalePos: Record<string, string> = {
['decrement']: '-.35 .25 -2',
['increment']: '.35 .25 -2',
['label']: '0 .2 -2',
};
// define spatial placement of y-scale tiles in a-scene
const yScalePos: Record<string, string> = {
['decrement']: '-.35 0 -2',
['increment']: '.35 0 -2',
['label']: '0 -.05 -2',
};
// define spatial placement of z-scale tiles in a-scene
const zScalePos: Record<string, string> = {
['decrement']: '-.35 -0.25 -2',
['increment']: '.35 -0.25 -2',
['label']: '0 -0.3 -2',
};
// define spatial placement of all-scale tiles in a-scene
const allScalePos: Record<string, string> = {
['decrement']: '-.35 -.5 -2',
['increment']: '.35 -.5 -2',
['label']: '0 -.55 -2',
};
// define spatial placement of ctrl panel toggle bar in a-scene
const toggleBarPos: Record<string, string> = {
['bar']: '0 -.725 -2',
['label']: '0 -.775 -1.98',
};
// define spatial placement of ctrl panel background in a-scene
const bckgrdPos: Record<string, string> = {
['place']: '0 -.1 -2.01',
};
// constants to add to corresponding scales after 'keydown' event listener
const scaleControls = {
9: {x: 10, y: 0, z: 0},
7: {x: -10, y: 0, z: 0},
6: {x: 0, y: 10, z: 0},
4: {x: 0, y: -10, z: 0},
3: {x: 0, y: 0, z: 10},
1: {x: 0, y: 0, z: -10},
8: {x: 10, y: 10, z: 10},
5: {x: -10, y: -10, z: -10},
};
const speedControls = {
Minus: {val: -.1},
Equal: {val: .1},
};
const mvmtControls = {
I: {dim: 'z', direction: 1},
K: {dim: 'z', direction: -1},
Q: {dim: 'y', direction: 1},
Z: {dim: 'y', direction: -1},
L: {dim: 'x', direction: 1},
J: {dim: 'x', direction: -1},
};
const dimensions = ['x', 'y', 'z'];
const ADD_SPEED = .1;
export class Controls{
showCtrls: boolean;
loadingFlag: boolean;
scatter: Scatterplot;
camera: HTMLElement;
keyEventsLoaded: boolean;
constructor(scatter: Scatterplot){
this.keyEventsLoaded = false;
this.loadingFlag = true;
this.showCtrls = true;
this.scatter = scatter;
this.camera = document.querySelector('[camera]');
if (this.camera === null){
document.createElement('a-camera');
}
this.createNavTiles();
this.createCtrlPanel();
this.addDemoKeys();
}
createNavTiles(){
for (const dimension of dimensions){
this.createNavTile(dimension);
}
}
// in web desktop version, there is no laser pointer to adjust ctrls/position
// addDemoKeys allows for user to still use all ctrls through keyboard interface
addDemoKeys(){
document.addEventListener('keydown', (event) => {
// add controls to increase/decrease speed of movement in space with either ijkl mvmt or daydream ctrl panel
for (const [sign, value] of Object.entries(speedControls)) {
if ([sign].includes(event.code)) {
console.log(value.val);
this.scatter.setDaydreamNavSpeed(this.scatter.getDaydreamNavSpeed() + value.val);
}
}
// add controls to change scale of x, y, z or all dimensions
for (const [digit, offsets] of Object.entries(scaleControls)) {
if (['Numpad' + digit, 'Digit' + digit].includes(event.code)) {
this.scatter.changeScales(this.scatter.getGridBound('x') + offsets.x,
this.scatter.getGridBound('y') + offsets.y,
this.scatter.getGridBound('z') + offsets.z);
}
}
// reset scale back to initial loading scale, where each point's distance from origin per dim. is from 0 to 50
if (event.code === 'Numpad0' || event.code === 'Digit0'){
this.scatter.changeScales(
50, 50, 50);
}
const camPos = document.querySelector('[camera]').object3D.position;
// create movement on keyboard with adjustable speed (speed ctrls diretly affect mvmt of ijkl + qz mvmt)
for (const [key, value] of Object.entries(mvmtControls)) {
if (['Key' + key].includes(event.code)) {
if (value.dim === 'x'){
camPos.set(
camPos.x + value.direction * this.scatter.DAYDREAM_NAV_SPEED,
camPos.y,
camPos.z
);
}
if (value.dim === 'y'){
camPos.set(
camPos.x,
camPos.y + value.direction * this.scatter.DAYDREAM_NAV_SPEED,
camPos.z
);
}
if (value.dim === 'z'){
camPos.set(
camPos.x,
camPos.y,
camPos.z + value.direction * this.scatter.DAYDREAM_NAV_SPEED
);
}
}
}
// toggle on/off the Control Panel
// since there is no laser in desktop version, must use key to toggle panel
if (event.code === 'KeyT'){
const nonTxtControlItems = document.getElementsByClassName('nonTextToggle');
for (const item of (nonTxtControlItems as unknown as Array<Element>)){
if (!this.showCtrls){
(item as AFRAME.Entity).setAttribute('scale', '1 1 1');
}
else{
// panel will obstruct cursor => scale size down to keep from entity intersection conflict
(item as AFRAME.Entity).setAttribute('scale', '.01 .01 .01');
}
}
const controlItems = document.getElementsByClassName('toggle');
for (const item of (controlItems as unknown as Array<Element>)){
// toggle on/off the ctrl panel
(item as AFRAME.Entity).setAttribute('visible', !this.showCtrls);
}
this.showCtrls = !this.showCtrls;
}
});
this.keyEventsLoaded = true;
}
// create 6 arrows - 3 per dimension - to allow for movement in scene
createNavTile(dim: string){
let rigPos: any;
if (document.getElementById('rig') as AFRAME.Entity != null){
rigPos = (document.getElementById('rig') as AFRAME.Entity).object3D.position;
}
else{
rigPos = document.createElement('a-camera').object3D.position;
}
const navTilePos = document.createElement('a-entity');
navTilePos.className = 'navigation';
this.cameraAppendChild(navTilePos);
const navTileNeg = document.createElement('a-entity');
navTileNeg.className = 'navigation';
this.cameraAppendChild(navTileNeg);
(navTilePos as AFRAME.Entity).setAttribute('geometry', 'primitive: plane; height: .75; width: .75');
(navTileNeg as AFRAME.Entity).setAttribute('geometry', 'primitive: plane; height: .75; width: .75');
let positivePos = '';
let negativePos = '';
let imagePos = '../assets/up_arrow.png';
let imageNeg = '../assets/down_arrow.png';
if (dim === 'x'){
positivePos = tilePos.xPos;
negativePos = tilePos.xNeg;
imagePos = '../assets/right_arrow.png';
imageNeg = '../assets/left_arrow.png';
} else if (dim === 'y'){
positivePos = tilePos.yPos;
negativePos = tilePos.yNeg;
} else if (dim === 'z'){
positivePos = tilePos.zPos;
negativePos = tilePos.zNeg;
}
(navTilePos as AFRAME.Entity).setAttribute('position', positivePos);
(navTileNeg as AFRAME.Entity).setAttribute('position', negativePos);
(navTilePos as AFRAME.Entity).setAttribute('material', `color: black; opacity: .95; src: ${imagePos}`);
(navTileNeg as AFRAME.Entity).setAttribute('material', `color: black; opacity: .95; src: ${imageNeg}`);
let intervalPos;
let intervalNeg;
// set event listeners with scatter.DAYDREAM... delta in order to have updated speeds
(navTilePos as AFRAME.Entity).addEventListener('mousedown', () => {
intervalPos = setInterval(() => {
let xDelta = 0;
let yDelta = 0;
let zDelta = 0;
if (dim === 'x'){
// add negative bc camera is turned around
xDelta = -this.scatter.DAYDREAM_NAV_SPEED;
} else if (dim === 'y'){
yDelta = this.scatter.DAYDREAM_NAV_SPEED;
} else if (dim === 'z'){
zDelta = this.scatter.DAYDREAM_NAV_SPEED;
}
rigPos.set(
rigPos.x + xDelta,
rigPos.y + yDelta,
rigPos.z + zDelta
);
}, 200);
});
(navTilePos as AFRAME.Entity).addEventListener('mouseup', () => { clearInterval(intervalPos); });
(navTileNeg as AFRAME.Entity).addEventListener('mousedown', () => {
intervalNeg = setInterval(() => {
let xDelta = 0;
let yDelta = 0;
let zDelta = 0;
if (dim === 'x'){
// add negative bc camera is turned around
xDelta = -this.scatter.DAYDREAM_NAV_SPEED;
} else if (dim === 'y'){
yDelta = this.scatter.DAYDREAM_NAV_SPEED;
} else if (dim === 'z'){
zDelta = this.scatter.DAYDREAM_NAV_SPEED;
}
rigPos.set(
rigPos.x - xDelta,
rigPos.y - yDelta,
rigPos.z - zDelta
);
}, 200);
});
(navTileNeg as AFRAME.Entity).addEventListener('mouseup', () => { clearInterval(intervalNeg); });
}
// abstracted calling to create collapsible control panel with speed and scale adjustments
createCtrlPanel(){
this.createBackground();
this.createSpeedCtrls('plus');
this.createSpeedCtrls('neg');
this.createSpeedCtrls('label');
for (const dimension of dimensions){
this.createScaleCtrls(dimension);
}
this.createScaleCtrls('all');
this.createToggleBar();
const elems = document.getElementsByClassName('toggle');
for (const elem of (elems as unknown as Array<Element>)){
(elem as AFRAME.Entity).setAttribute('visible', false);
}
}
// create speedctrls and 'speed' label based on sign parameter
createSpeedCtrls(sign: string){
const speedTile = document.createElement('a-entity') as AFRAME.Entity;
speedTile.className = 'toggle nonTextToggle speed';
this.cameraAppendChild(speedTile);
if (sign === 'plus'){
speedTile.setAttribute('geometry', 'primitive: plane; height: .25; width: .25');
speedTile.setAttribute('position', speedPos.plus);
speedTile.setAttribute('material', 'color: black; opacity: .95; src: ../assets/plus.png;');
(speedTile as AFRAME.Entity).addEventListener('mousedown', () => {
this.scatter.setDaydreamNavSpeed(this.scatter.getDaydreamNavSpeed() + ADD_SPEED);
});
} else if (sign === 'neg'){
speedTile.setAttribute('geometry', 'primitive: plane; height: .25; width: .25');
speedTile.setAttribute('position', speedPos.minus);
speedTile.setAttribute('material', 'color: black; opacity: .95; src: ../assets/negative.png');
(speedTile as AFRAME.Entity).addEventListener('mousedown', () => {
if (this.scatter.DAYDREAM_NAV_SPEED > 0){
this.scatter.setDaydreamNavSpeed(this.scatter.getDaydreamNavSpeed() - ADD_SPEED);
}
});
}
else if (sign === 'label'){
const speedLabelTile = document.createElement('a-entity') as AFRAME.Entity;
this.cameraAppendChild(speedLabelTile);
speedLabelTile.className = 'toggle';
speedLabelTile.setAttribute('position', speedPos.label);
speedLabelTile.setAttribute('text', `value: Speed; align: center; color: black; shader: msdf; font: ${ROBOTO};`);
speedLabelTile.setAttribute('scale', '2 2 1');
}
}
// make positive and negative tile for scale adjustment
// dim: string defines unique attributes of tile, such as src picture, position, which dimension it is re-scaling, etc
createScaleCtrls(dim: string){
const scaleTilePos = document.createElement('a-entity') as AFRAME.Entity;
scaleTilePos.className = 'toggle nonTextToggle';
this.cameraAppendChild(scaleTilePos);
const scaleTileNeg = document.createElement('a-entity') as AFRAME.Entity;
scaleTileNeg.className = 'toggle nonTextToggle';
this.cameraAppendChild(scaleTileNeg);
const labelTile = document.createElement('a-entity') as AFRAME.Entity;
labelTile.className = 'toggle';
this.cameraAppendChild(labelTile);
scaleTilePos.setAttribute('geometry', 'primitive: plane; height: .25; width: .25');
scaleTilePos.setAttribute('material', 'color: black; opacity: .95; src: ../assets/plus.png;');
scaleTileNeg.setAttribute('geometry', 'primitive: plane; height: .25; width: .25');
scaleTileNeg.setAttribute('material', 'color: black; opacity: .95; src: ../assets/negative.png;');
let xScaleDelta = 0;
let yScaleDelta = 0;
let zScaleDelta = 0;
let positivePos = '';
let negativePos = '';
let labelPos = '';
let labelName = '';
if (dim === 'x'){
xScaleDelta = 10;
positivePos = xScalePos.increment;
negativePos = xScalePos.decrement;
labelPos = xScalePos.label;
labelName = 'X-Scale';
} else if (dim === 'y'){
yScaleDelta = 10;
positivePos = yScalePos.increment;
negativePos = yScalePos.decrement;
labelPos = yScalePos.label;
labelName = 'Y-Scale';
} else if (dim === 'z'){
zScaleDelta = 10;
positivePos = zScalePos.increment;
negativePos = zScalePos.decrement;
labelPos = zScalePos.label;
labelName = 'Z-Scale';
} else if (dim === 'all'){
// if all scales chosen, then defaults/resets to scale of x bounds
xScaleDelta = 10;
positivePos = allScalePos.increment;
negativePos = allScalePos.decrement;
labelPos = allScalePos.label;
labelName = 'XYZ-Scale';
}
scaleTilePos.setAttribute('position', positivePos);
(scaleTilePos as AFRAME.Entity).addEventListener('mousedown', () => {
if (dim === 'all'){
this.scatter.changeScales(
this.scatter.getGridBound('x') + xScaleDelta,
this.scatter.getGridBound('x') + xScaleDelta,
this.scatter.getGridBound('x') + xScaleDelta);
} else {
this.scatter.changeScales(
this.scatter.getGridBound('x') + xScaleDelta,
this.scatter.getGridBound('y') + yScaleDelta,
this.scatter.getGridBound('z') + zScaleDelta);
}
});
scaleTileNeg.setAttribute('position', negativePos);
(scaleTileNeg as AFRAME.Entity).addEventListener('mousedown', () => {
if (dim === 'all'){
this.scatter.changeScales(
this.scatter.getGridBound('x') - xScaleDelta,
this.scatter.getGridBound('x') - xScaleDelta,
this.scatter.getGridBound('x') - xScaleDelta);
} else {
this.scatter.changeScales(
this.scatter.getGridBound('x') - xScaleDelta,
this.scatter.getGridBound('y') - yScaleDelta,
this.scatter.getGridBound('z') - zScaleDelta);
}
});
labelTile.setAttribute('position', labelPos);
labelTile.setAttribute('text', `value: ${labelName}; align: center; color: black; shader: msdf; font: ${ROBOTO};`);
labelTile.setAttribute('scale', '2 2 1');
}
// create bar to toggle control panel on/off in desktop (KeyT) or mobile version (mousedown)
createToggleBar(){
const toggleBar = document.createElement('a-entity');
toggleBar.className = 'toggleBar';
this.cameraAppendChild(toggleBar);
toggleBar.setAttribute('geometry', 'primitive: plane; height: .15; width:.75');
toggleBar.setAttribute('material', 'color: #4385f4; opacity: 1');
toggleBar.setAttribute('position', toggleBarPos.bar);
const toggleText = document.createElement('a-entity');
this.cameraAppendChild(toggleText);
toggleText.setAttribute('position', toggleBarPos.label);
toggleText.setAttribute('text', `value: Control Panel; align: center; color: black; shader: msdf; font: ${ROBOTO};`);
toggleText.setAttribute('scale', '2 2 1');
toggleBar.addEventListener('mousedown', () => {
const nonTxtControlItems = document.getElementsByClassName('nonTextToggle');
for (const item of (nonTxtControlItems as unknown as Array<Element>)){
if (!this.showCtrls){
(item as AFRAME.Entity).setAttribute('scale', '1 1 1');
}
else{
(item as AFRAME.Entity).setAttribute('scale', '.01 .01 .01');
}
}
const controlItems = document.getElementsByClassName('toggle');
for (const item of (controlItems as unknown as Array<Element>)){
(item as AFRAME.Entity).setAttribute('visible', !this.showCtrls);
}
this.showCtrls = !this.showCtrls;
});
}
createBackground(){
const bckgrd = document.createElement('a-entity');
bckgrd.className = 'background';
this.cameraAppendChild(bckgrd);
bckgrd.className = 'toggle nonTextToggle';
bckgrd.setAttribute('geometry', 'primitive: plane; height: 1.5; width: 1');
bckgrd.setAttribute('material', 'color: #4385f4; opacity: .75;');
bckgrd.setAttribute('position', bckgrdPos.place);
}
// check to make sure camera is not null before trying to append a child to it
private cameraAppendChild(element: HTMLElement){
if (this.camera !== null){
this.camera.appendChild(element);
}
else {
this.scatter.container!.appendChild(element);
}
}
}