-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnewthumbcontrols.js
More file actions
323 lines (282 loc) · 9.48 KB
/
newthumbcontrols.js
File metadata and controls
323 lines (282 loc) · 9.48 KB
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
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
typeof define === 'function' && define.amd ? define(factory) :
(factory());
}(this, (function () { 'use strict';
var MAX_LOGS = null;
function VR_LOG(data) {
console.log(data)
const vr_console = document.getElementById('vr_console');
if (vr_console) {
const lines = vr_console.getAttribute('value').split('\n');
lines.push(JSON.stringify(data));
if (MAX_LOGS && lines.length > MAX_LOGS) {
lines.shift()
}
vr_console.setAttribute('value', lines.join('\n'));
}
}
window.onerror = e => VR_LOG("error" + e);
var TYPE_PAD = 'PAD';
var TYPE_STICK = 'STICK';
var ANGLE_RIGHT = 0;
var ANGLE_UP = 90;
var ANGLE_LEFT = 180;
var ANGLE_DOWN = 270;
var RIGHT = 'right';
var UP = 'up';
var LEFT = 'left';
var DOWN = 'down';
var ANGLES = [ANGLE_RIGHT, ANGLE_UP, ANGLE_LEFT, ANGLE_DOWN];
var DIRECTIONS = [RIGHT, UP, LEFT, DOWN];
var EVENTS = {NULL: {START: 'thumbstart', END: 'thumbend'}};
DIRECTIONS.forEach(direction => {
EVENTS[direction] = {};
EVENTS[direction].START = 'thumb' + direction + 'start';
EVENTS[direction].END = 'thumb' + direction + 'end';
});
// For debug.
var SIZE = 240;
/**
* Normalize trackpad vs thumbstick controls.
* `thumbstart`
* `thumbend`
* `thumbleftstart`
* `thumbleftend`
* `thumbrightstart`
* `thumbrightend`
* `thumbupstart`
* `thumbupend`
* `thumbdownstart`
* `thumbdownend`
*/
AFRAME.registerComponent('thumb-controls', {
dependencies: ['tracked-controls'],
schema: {
thresholdAngle: {default: 89.5},
thresholdPad: {default: 0.05},
thresholdStick: {default: 0.75}
},
init: function () {
var el = this.el;
VR_LOG('newthumbcontrols init')
this.onTrackpadDown = this.onTrackpadDown.bind(this);
this.onTrackpadUp = this.onTrackpadUp.bind(this);
this.directionStick = '';
this.directionTrackpad = '';
// Get thumb type (stick vs pad).
this.type = TYPE_STICK;
el.addEventListener('controllerconnected', evt => {
VR_LOG("newthumbcontrols: controllerconnected");
VR_LOG(evt.detail.name + ', hand: ' + evt.detail.component.data.hand);
if (evt.detail.name === 'generic-tracked-controller-controls')
return;
if (evt.detail.name === 'oculus-touch-controls' ||
evt.detail.name === 'windows-motion-controls') {
this.type = TYPE_STICK;
VR_LOG('type = TYPE_STICK');
return;
}
this.type = TYPE_PAD;
VR_LOG('type = TYPE_PAD');
});
this.axis = el.components['tracked-controls'].axis;
},
play: function () {
var el = this.el;
el.addEventListener('trackpaddown', this.onTrackpadDown);
el.addEventListener('trackpadup', this.onTrackpadUp);
el.addEventListener('touchpaddown', this.onTrackpadDown);
el.addEventListener('touchpadup', this.onTrackpadUp);
},
pause: function () {
var el = this.el;
el.removeEventListener('trackpaddown', this.onTrackpadDown);
el.removeEventListener('trackpadup', this.onTrackpadUp);
el.removeEventListener('touchpaddown', this.onTrackpadDown);
el.removeEventListener('touchpadup', this.onTrackpadUp);
},
// For pad.
onTrackpadDown: function () {
console.log('In onTrackpadDown()');
var direction;
var el = this.el;
if (this.getDistance() < this.data.thresholdPad) { return; }
direction = this.getDirection();
if (!direction) { return; }
this.directionTrackpad = direction;
el.emit(EVENTS.NULL.START, null, false);
el.emit(EVENTS[this.directionTrackpad].START, null, false);
console.log('event: ', EVENTS[this.directionTrackpad].START)
},
// For pad.
onTrackpadUp: function () {
var el = this.el;
if (!this.directionTrackpad) { return; }
el.emit(EVENTS.NULL.END, null, false);
el.emit(EVENTS[this.directionTrackpad].END, null, false);
console.log('event: ', EVENTS[this.directionTrackpad].END)
this.directionTrackpad = '';
},
// Axis.
tick: function () {
var direction;
var el = this.el;
if (this.type === TYPE_PAD) { return; }
// Stick pulled. Store direction and emit start event.
if (!this.directionStick && this.getDistance() > this.data.thresholdStick) {
direction = this.getDirection();
VR_LOG('In tick(), direction set to ' + direction);
if (!direction) { return; }
this.directionStick = direction;
el.emit(EVENTS.NULL.START, null, false);
el.emit(EVENTS[this.directionStick].START, null, false);
VR_LOG('event: ' + EVENTS[this.directionStick].START)
return;
}
// Stick pulled back. Reset direciton and emit end event.
if (this.directionStick && this.getDistance() < this.data.thresholdStick) {
el.emit(EVENTS.NULL.END, null, false);
el.emit(EVENTS[this.directionStick].END, null, false);
VR_LOG('event: ' + EVENTS[this.directionStick].END)
this.directionStick = '';
}
},
/**
* Distance from center of thumb.
*/
getDistance: function () {
var axis = this.axis;
if (this.type === TYPE_PAD) {
return Math.sqrt(axis[1] * axis[1] + axis[0] * axis[0]);
} else {
//const d = Math.sqrt(axis[3] * axis[3] + axis[2] * axis[2]);
//if (d > 0 && d !== this.prevD) {
// VR_LOG('distance = ' + d)
// this.prevD = d
//}
return Math.sqrt(axis[3] * axis[3] + axis[2] * axis[2]);
}
},
/**
* Translate angle into direction.
*/
getDirection: function () {
var angle;
var bottomThreshold;
var i;
var threshold;
var topThreshold;
angle = this.getAngle();
threshold = this.data.thresholdAngle / 2;
for (i = 0; i < ANGLES.length; i++) {
topThreshold = ANGLES[i] + threshold;
if (topThreshold > 360) { topThreshold = topThreshold - 360; }
bottomThreshold = ANGLES[i] - threshold;
if (bottomThreshold < 0) {
if ((angle >= 360 + bottomThreshold && angle <= 360) ||
(angle >= 0 && angle <= topThreshold)) {
return DIRECTIONS[i];
}
}
if (angle >= bottomThreshold && angle <= topThreshold) {
return DIRECTIONS[i];
}
}
},
/**
* Get angle in degrees, with 0 starting on the right going to 360. Like unit circle.
*/
getAngle: function () {
var angle;
var axis = this.axis;
var flipY;
flipY = this.type === TYPE_STICK ? -1 : 1;
if (this.type === TYPE_PAD) {
angle = Math.atan2(axis[1] * flipY, axis[0]);
} else {
angle = Math.atan2(axis[3] * flipY, axis[2]);
}
if (angle < 0) { angle = 2 * Math.PI + angle; }
return THREE.Math.radToDeg(angle);
}
});
AFRAME.registerComponent('thumb-controls-debug', {
dependencies: ['thumb-controls', 'tracked-controls'],
schema: {
controllerType: {type: 'string'},
hand: {type: 'string'},
enabled: {default: false}
},
init: function () {
var isActive;
var axis;
var axisMoveEventDetail;
var canvas;
var el = this.el;
var data = this.data;
if (!data.enabled && !AFRAME.utils.getUrlParameter('debug-thumb')) { return; }
console.log('%c debug-thumb', 'background: #111; color: red');
// Stub.
el.components['tracked-controls'].handleAxes = () => {};
axis = [0, 0, 0];
axisMoveEventDetail = {axis: axis};
el.components['tracked-controls'].axis = axis;
el.components['thumb-controls'].axis = axis;
canvas = this.createCanvas();
canvas.addEventListener('click', evt => {
if (this.data.controllerType === 'vive-controls') {
if (isActive) {
el.emit('trackpadup');
} else {
el.emit('trackpaddown');
}
} else {
if (isActive) {
axis[0] = 0;
axis[1] = 0;
el.emit('axismove', axisMoveEventDetail, false);
}
}
isActive = !isActive;
});
canvas.addEventListener('mousemove', evt => {
var rect;
if (!isActive) { return; }
rect = canvas.getBoundingClientRect();
axis[0] = (evt.clientX - rect.left) / SIZE * 2 - 1;
axis[1] = (evt.clientY - rect.top) / SIZE * 2 - 1;
el.emit('axismove', axisMoveEventDetail, false);
});
canvas.addEventListener('mouseleave', evt => {
if (!isActive) { return; }
axis[0] = 0;
axis[1] = 0;
el.emit('axismove', axisMoveEventDetail, false);
});
},
createCanvas: function () {
var canvas;
var ctx;
canvas = document.createElement('canvas');
canvas.classList.add('debugThumb');
canvas.height = SIZE;
canvas.width = SIZE;
canvas.style.bottom = 0;
canvas.style.borderRadius = '250px';
canvas.style.opacity = 0.5;
canvas.style.position = 'fixed';
canvas.style.zIndex = 999999999;
if (this.data.hand === 'left') {
canvas.style.left = 0;
} else {
canvas.style.right = 0;
}
ctx = canvas.getContext('2d');
ctx.fillStyle = "#333";
ctx.fillRect(0, 0, SIZE, SIZE);
document.body.appendChild(canvas);
return canvas;
}
});
})));