-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtouch.js
148 lines (145 loc) · 5.63 KB
/
touch.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
let usingTouch = false;
let touchMovement = null;
let canSimKeys;
function initTouch() {
const touchUI = document.getElementById('touch-ui');
const touchCircle = document.getElementById('touch-circle');
const breathePad = document.getElementById('breathe-interaction');
const simKeys = {}
for (const simKey of document.querySelectorAll('[data-sim-key]')) {
simKeys[simKey.dataset.simKey] = simKey;
}
canSimKeys = (keys = null) => {
if (keys && keys.includes('arms')) {
keys.push('exp-up', 'exp-down', 'power-up', 'power-down', 'reset');
}
for (const [simKey, elem] of Object.entries(simKeys)) {
if (!keys || keys.includes(simKey)) {
elem.classList.add('available');
} else {
elem.classList.remove('available');
}
}
}
let cameraRotator = null;
let movement = null;
let breather = null;
let optionClick = null;
function calcMovementFromTouch(touch) {
touchMovement = new THREE.Vector2((touch.clientX - movement.centreX) / 40, (touch.clientY - movement.centreY) / 40);
if (touchMovement.lengthSq() > 1) touchMovement.normalize();
touchCircle.style.setProperty('--x', touchMovement.x * 40 + 'px');
touchCircle.style.setProperty('--y', touchMovement.y * 40 + 'px');
}
document.addEventListener('touchstart', e => {
if (e.target.closest('.clickable')) return;
if (!usingTouch) {
usingTouch = true;
document.body.classList.add('using-touch');
document.addEventListener('touchend', userInteracted, {once: true});
}
if (!document.body.classList.contains('hide-options')) {
document.body.classList.add('hide-options');
}
document.body.classList.add('hide-options');
for (const touch of e.changedTouches) {
if (touch.target.classList.contains('touch-target')) {
if (touch.target === touchCircle && !movement) {
const rect = touchCircle.getBoundingClientRect();
movement = {
identifier: touch.identifier,
centreX: rect.left + rect.width / 2,
centreY: rect.top + rect.height / 2
};
touchCircle.classList.add('moving');
calcMovementFromTouch(touch);
} else if (touch.target === breathePad) {
breathePad.classList.add('breathing');
const rect = breathePad.getBoundingClientRect();
if (touch.clientY - rect.top < rect.height / 2) {
keys.inhale = true;
keys.exhale = false;
breathePad.classList.add('inhaling');
breathePad.classList.remove('exhaling');
} else {
keys.inhale = false;
keys.exhale = true;
breathePad.classList.remove('inhaling');
breathePad.classList.add('exhaling');
}
breather = {
identifier: touch.identifier,
rect
};
} else if (touch.target.dataset.simKey) {
touch.target.classList.add('pressed');
const key = touch.target.dataset.simKey;
keys[key] = true;
if (onKeyPress[key]) onKeyPress[key]();
} else if (touch.target.classList.contains('options-btn')) {
touch.target.classList.add('pressed');
optionClick = touch.identifier;
}
} else if (!cameraRotator) {
cameraRotator = {
identifier: touch.identifier,
lastX: touch.clientX,
lastY: touch.clientY
};
}
}
e.preventDefault();
}, {passive: false});
document.addEventListener('touchmove', e => {
if (e.target.closest('.clickable')) return;
for (const touch of e.changedTouches) {
if (cameraRotator && cameraRotator.identifier === touch.identifier) {
rotateCamera((touch.clientX - cameraRotator.lastX) / options.touchSensitivity, (touch.clientY - cameraRotator.lastY) / options.touchSensitivity);
cameraRotator.lastX = touch.clientX;
cameraRotator.lastY = touch.clientY;
} else if (movement && movement.identifier === touch.identifier) {
calcMovementFromTouch(touch);
} else if (breather && breather.identifier === touch.identifier) {
if (touch.clientY - breather.rect.top < breather.rect.height / 2) {
keys.inhale = true;
keys.exhale = false;
breathePad.classList.add('inhaling');
breathePad.classList.remove('exhaling');
} else {
keys.inhale = false;
keys.exhale = true;
breathePad.classList.remove('inhaling');
breathePad.classList.add('exhaling');
}
}
}
e.preventDefault();
}, {passive: false});
document.addEventListener('touchend', e => {
if (e.target.closest('.clickable')) return;
for (const touch of e.changedTouches) {
if (cameraRotator && cameraRotator.identifier === touch.identifier) {
cameraRotator = null;
} else if (movement && movement.identifier === touch.identifier) {
touchMovement = null;
movement = null;
touchCircle.classList.remove('moving');
} else if (breather && breather.identifier === touch.identifier) {
keys.inhale = false;
keys.exhale = false;
breathePad.classList.remove('breathing');
breathePad.classList.remove('inhaling');
breathePad.classList.remove('exhaling');
breather = null;
} else if (touch.target.dataset.simKey) {
touch.target.classList.remove('pressed');
keys[touch.target.dataset.simKey] = false;
} else if (optionClick === touch.identifier) {
touch.target.classList.remove('pressed');
document.body.classList.remove('hide-options');
optionClick = null;
}
}
e.preventDefault();
}, {passive: false});
}