-
Notifications
You must be signed in to change notification settings - Fork 1
/
main-app.js
119 lines (95 loc) · 3.46 KB
/
main-app.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
// @ts-check
import { Demo3DObj } from './demo-3dobj.js';
import { SpaceDriver } from './space-driver.js';
export class MainApp extends HTMLElement {
/** @type {Demo3dobj} */ #obj
#cells
constructor() {
super();
this.#cells = [];
this.handleTranslate = this.handleTranslate.bind(this);
this.handleRotate = this.handleRotate.bind(this);
this.handleButtons = this.handleButtons.bind(this);
this.handleConnect = this.handleConnect.bind(this);
this.handleDisconnect = this.handleDisconnect.bind(this);
}
connectedCallback() {
this.innerHTML = `
<style>
#list {
display: grid;
grid-template-columns: 1fr 1fr;
width: 300px;
}
</style>
<button id='connect'>CONNECT</button>
<h2>Status: <span id='status'> - </span></h2>
<div id='list'></div>
<demo-3dobj></demo-3dobj>
`;
this.#obj = this.querySelector('demo-3dobj');
this.querySelector('#connect').addEventListener('click', this.doScan);
this._initList();
SpaceDriver.addEventListener('translate', this.handleTranslate);
SpaceDriver.addEventListener('rotate', this.handleRotate);
SpaceDriver.addEventListener('buttons', this.handleButtons);
SpaceDriver.addEventListener('connect', this.handleConnect);
SpaceDriver.addEventListener('disconnect', this.handleDisconnect);
}
disconnectedCallback() {
SpaceDriver.removeEventListener('translate', this.handleTranslate);
SpaceDriver.removeEventListener('rotate', this.handleRotate);
SpaceDriver.removeEventListener('buttons', this.handleButtons);
SpaceDriver.removeEventListener('connect', this.handleConnect);
SpaceDriver.removeEventListener('disconnect', this.handleDisconnect);
}
_initList() {
const list = this.querySelector('#list');
const labels = ['X', 'Y', 'Z', 'Pitch', 'Roll', 'Yaw', 'Buttons'];
labels.forEach(l => {
const label = document.createElement('span');
label.classList.add('label');
label.innerText = l;
const value = document.createElement('span');
value.classList.add('value');
value.innerText = `-`;
this.#cells.push(value);
list.append(label, value);
});
}
setStatus(str) {
this.querySelector('#status').innerHTML = str;
}
setCellValue(i, val) {
this.#cells[i].innerText = val;
}
doScan() {
SpaceDriver.scan();
}
handleTranslate(/** @type {CustomEvent} */ evt) {
const {x, y, z} = evt.detail;
this.#obj.setTranslation(x, y, -z);
this.setCellValue(0, x);
this.setCellValue(1, y);
this.setCellValue(2, z);
}
handleRotate(/** @type {CustomEvent} */ evt) {
const {rx, ry, rz} = evt.detail;
this.#obj.setRotation(rx/5, ry/5, rz/5);
this.setCellValue(3, rx);
this.setCellValue(4, ry);
this.setCellValue(5, rz);
}
handleButtons(/** @type {CustomEvent} */ evt) {
const {buttons} = evt.detail;
this.setCellValue(6, buttons.join(', '));
}
handleConnect(/** @type {CustomEvent} */ evt) {
const {device} = evt.detail;
this.setStatus(`${device.productName} connected`);
}
handleDisconnect() {
this.setStatus(` - `);
}
}
customElements.define('main-app', MainApp);