-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathlogitech-mx-ink-controls.js
172 lines (149 loc) · 5.67 KB
/
logitech-mx-ink-controls.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
import * as THREE from 'three';
import { registerComponent } from '../core/component.js';
import { AFRAME_CDN_ROOT } from '../constants/index.js';
import { checkControllerPresentAndSetup, emitIfAxesChanged, onButtonEvent } from '../utils/tracked-controls.js';
// See Profiles Registry:
// https://github.com/immersive-web/webxr-input-profiles/tree/master/packages/registry
// TODO: Add a more robust system for deriving gamepad name.
var GAMEPAD_ID = 'logitech-mx-ink';
var LOGITECH_MX_INK_MODEL_GLB_BASE_URL = AFRAME_CDN_ROOT + 'controllers/logitech/';
/**
* Button IDs:
* 0 - trigger
* 1 - squeeze
* 5 - touchpad
* 6 - tip
* 7 - dock
*
*/
var INPUT_MAPPING_WEBXR = {
left: {
buttons: ['front', 'back', 'none', 'none', 'none', 'tip']
},
right: {
buttons: ['front', 'back', 'none', 'none', 'none', 'tip']
}
};
/**
* Logitech MX Ink Controls
*/
export var Component = registerComponent('logitech-mx-ink-controls', {
schema: {
hand: {default: 'left'},
model: {default: true},
orientationOffset: {type: 'vec3'}
},
mapping: INPUT_MAPPING_WEBXR,
init: function () {
var self = this;
this.onButtonChanged = this.onButtonChanged.bind(this);
this.onButtonDown = function (evt) { onButtonEvent(evt.detail.id, 'down', self, self.data.hand); };
this.onButtonUp = function (evt) { onButtonEvent(evt.detail.id, 'up', self, self.data.hand); };
this.onButtonTouchEnd = function (evt) { onButtonEvent(evt.detail.id, 'touchend', self, self.data.hand); };
this.onButtonTouchStart = function (evt) { onButtonEvent(evt.detail.id, 'touchstart', self, self.data.hand); };
this.bindMethods();
},
play: function () {
this.checkIfControllerPresent();
this.addControllersUpdateListener();
},
pause: function () {
this.removeEventListeners();
this.removeControllersUpdateListener();
},
bindMethods: function () {
this.onModelLoaded = this.onModelLoaded.bind(this);
this.onControllersUpdate = this.onControllersUpdate.bind(this);
this.checkIfControllerPresent = this.checkIfControllerPresent.bind(this);
this.removeControllersUpdateListener = this.removeControllersUpdateListener.bind(this);
this.onAxisMoved = this.onAxisMoved.bind(this);
},
addEventListeners: function () {
var el = this.el;
el.addEventListener('buttonchanged', this.onButtonChanged);
el.addEventListener('buttondown', this.onButtonDown);
el.addEventListener('buttonup', this.onButtonUp);
el.addEventListener('touchstart', this.onButtonTouchStart);
el.addEventListener('touchend', this.onButtonTouchEnd);
el.addEventListener('axismove', this.onAxisMoved);
el.addEventListener('model-loaded', this.onModelLoaded);
this.controllerEventsActive = true;
},
removeEventListeners: function () {
var el = this.el;
el.removeEventListener('buttonchanged', this.onButtonChanged);
el.removeEventListener('buttondown', this.onButtonDown);
el.removeEventListener('buttonup', this.onButtonUp);
el.removeEventListener('touchstart', this.onButtonTouchStart);
el.removeEventListener('touchend', this.onButtonTouchEnd);
el.removeEventListener('axismove', this.onAxisMoved);
el.removeEventListener('model-loaded', this.onModelLoaded);
el.sceneEl.removeEventListener('enter-vr', this.onEnterVR);
el.sceneEl.removeEventListener('exit-vr', this.onExitVR);
this.controllerEventsActive = false;
},
checkIfControllerPresent: function () {
var controllerObject3D = this.controllerObject3D;
if (controllerObject3D) { controllerObject3D.visible = false; }
checkControllerPresentAndSetup(this, GAMEPAD_ID, {
hand: this.data.hand,
iterateControllerProfiles: true
});
},
injectTrackedControls: function () {
var el = this.el;
var data = this.data;
var id = GAMEPAD_ID;
el.setAttribute('tracked-controls', {
id: id,
hand: data.hand,
handTrackingEnabled: false,
iterateControllerProfiles: true,
orientationOffset: data.orientationOffset
});
this.loadModel();
},
loadModel: function () {
var controllerObject3D = this.controllerObject3D;
if (!this.data.model) { return; }
if (controllerObject3D) {
controllerObject3D.visible = this.el.sceneEl.is('vr-mode');
this.el.setObject3D('mesh', controllerObject3D);
return;
}
this.el.setAttribute('gltf-model', LOGITECH_MX_INK_MODEL_GLB_BASE_URL + 'logitech-mx-ink.glb');
},
addControllersUpdateListener: function () {
this.el.sceneEl.addEventListener('controllersupdated', this.onControllersUpdate, false);
},
removeControllersUpdateListener: function () {
this.el.sceneEl.removeEventListener('controllersupdated', this.onControllersUpdate, false);
},
onControllersUpdate: function () {
this.checkIfControllerPresent();
},
onButtonChanged: function (evt) {
var button = this.mapping[this.data.hand].buttons[evt.detail.id];
var analogValue;
if (!button) { return; }
if (button === 'trigger') {
analogValue = evt.detail.state.value;
console.log('analog value of trigger press: ' + analogValue);
}
// Pass along changed event with button state, using button mapping for convenience.
this.el.emit(button + 'changed', evt.detail.state);
},
onModelLoaded: function (evt) {
if (evt.target !== this.el || !this.data.model) { return; }
this.el.emit('controllermodelready', {
name: 'logitech-mx-ink-controls',
model: this.data.model,
rayOrigin: new THREE.Vector3(0, 0, 0)
});
this.controllerObject3D = this.el.getObject3D('mesh');
this.controllerObject3D.visible = this.el.sceneEl.is('vr-mode');
},
onAxisMoved: function (evt) {
emitIfAxesChanged(this, this.mapping.axes, evt);
}
});