Skip to content

Commit fd90434

Browse files
committed
Move controller detection logic from polling to use xrSession inputsourceschange event
1 parent 0161e1d commit fd90434

16 files changed

+81
-70
lines changed

‎examples/showcase/painter/components/brush.js‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ AFRAME.registerComponent('brush', {
1414
this.painting = false;
1515
this.stroke = null;
1616
this.buttonsDown = 0;
17+
this.touches = 0;
18+
19+
this.onTouchStarted = this.onTouchStarted.bind(this);
20+
el.addEventListener('tiptouchstart', this.onTouchStarted);
21+
this.onTouchEnded = this.onTouchEnded.bind(this);
22+
el.addEventListener('tiptouchend', this.onTouchEnded);
1723

1824
this.onButtonDown = this.onButtonDown.bind(this);
1925
el.addEventListener('buttondown', this.onButtonDown);
20-
el.addEventListener('touchstart', this.onButtonDown);
21-
2226
this.onButtonUp = this.onButtonUp.bind(this);
2327
el.addEventListener('buttonup', this.onButtonUp);
24-
el.addEventListener('touchend', this.onButtonUp);
2528

2629
this.onControllerConnected = this.onControllerConnected.bind(this);
2730
el.addEventListener('controllerconnected', this.onControllerConnected);
@@ -35,6 +38,19 @@ AFRAME.registerComponent('brush', {
3538
this.controllerName = evt.detail.name;
3639
},
3740

41+
onTouchStarted: function (evt) {
42+
if (!this.data.enabled) { return; }
43+
this.startNewStroke();
44+
this.painting = true;
45+
},
46+
47+
onTouchEnded: function (evt) {
48+
if (!this.data.enabled) { return; }
49+
if (!this.painting) { return; }
50+
this.stroke = null;
51+
this.painting = false;
52+
},
53+
3854
onButtonDown: function () {
3955
if (!this.data.enabled) { return; }
4056
this.buttonsDown++;

‎examples/showcase/painter/index.html‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
<a-entity environment hide-on-enter-ar></a-entity>
2222
<a-entity
2323
id="right-hand"
24+
hand-tracking-controls="hand: right"
2425
brush="hand: right">
2526
</a-entity>
2627
<a-entity
2728
id="left-hand"
29+
hand-tracking-controls="hand: left"
2830
brush="hand: left">
2931
</a-entity>
3032
</a-scene>

‎src/components/generic-tracked-controller-controls.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ module.exports.Component = registerComponent('generic-tracked-controller-control
6666
this.onButtonTouchEnd = function (evt) { onButtonEvent(evt.detail.id, 'touchend', self); };
6767
this.controllerPresent = false;
6868
this.wasControllerConnected = false;
69-
this.lastControllerCheck = 0;
7069
this.bindMethods();
7170

7271
// generic-tracked-controller-controls has the lowest precedence.

‎src/components/hand-tracking-controls.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ module.exports.Component = registerComponent('hand-tracking-controls', {
112112
this.el.sceneEl.addEventListener('exit-vr', this.updateReferenceSpace);
113113
this.el.addEventListener('child-attached', this.onChildAttached);
114114

115-
this.el.object3D.visible = false;
116115
this.wristObject3D.visible = false;
117116
},
118117

‎src/components/hp-mixed-reality-controls.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ module.exports.Component = registerComponent('hp-mixed-reality-controls', {
5252
init: function () {
5353
var self = this;
5454
this.controllerPresent = false;
55-
this.lastControllerCheck = 0;
5655
this.onButtonChanged = this.onButtonChanged.bind(this);
5756
this.onButtonDown = function (evt) { onButtonEvent(evt.detail.id, 'down', self, self.data.hand); };
5857
this.onButtonUp = function (evt) { onButtonEvent(evt.detail.id, 'up', self, self.data.hand); };

‎src/components/logitech-mx-ink-controls.js‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,14 @@ module.exports.Component = registerComponent('logitech-mx-ink-controls', {
117117
orientationOffset: data.orientationOffset,
118118
space: 'gripSpace'
119119
});
120-
// Load model.
120+
this.loadModel();
121+
},
122+
123+
loadModel: function () {
121124
if (!this.data.model) { return; }
125+
if (this.controllerObject3D) {
126+
this.controllerObject3D.visible = this.el.sceneEl.is('vr-mode');
127+
}
122128
this.el.setAttribute('gltf-model', LOGITECH_MX_INK_MODEL_GLB_BASE_URL + 'logitech-mx-ink.glb');
123129
},
124130

@@ -158,8 +164,10 @@ module.exports.Component = registerComponent('logitech-mx-ink-controls', {
158164
rayOrigin: new THREE.Vector3(0, 0, 0)
159165
});
160166

167+
this.controllerObject3D = this.el.getObject3D('mesh');
168+
161169
if (this.el.sceneEl.is('ar-mode')) {
162-
this.el.getObject3D('mesh').visible = false;
170+
this.controllerObject3D.visible = false;
163171
}
164172
},
165173

‎src/components/magicleap-controls.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ module.exports.Component = registerComponent('magicleap-controls', {
4747
init: function () {
4848
var self = this;
4949
this.controllerPresent = false;
50-
this.lastControllerCheck = 0;
5150
this.onButtonChanged = this.onButtonChanged.bind(this);
5251
this.onButtonDown = function (evt) { onButtonEvent(evt.detail.id, 'down', self); };
5352
this.onButtonUp = function (evt) { onButtonEvent(evt.detail.id, 'up', self); };

‎src/components/oculus-go-controls.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ module.exports.Component = registerComponent('oculus-go-controls', {
6060
this.onButtonTouchStart = function (evt) { onButtonEvent(evt.detail.id, 'touchstart', self); };
6161
this.onButtonTouchEnd = function (evt) { onButtonEvent(evt.detail.id, 'touchend', self); };
6262
this.controllerPresent = false;
63-
this.lastControllerCheck = 0;
6463
this.bindMethods();
6564
},
6665

‎src/components/oculus-touch-controls.js‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ module.exports.Component = registerComponent('oculus-touch-controls', {
144144
this.onThumbstickMoved = this.onThumbstickMoved.bind(this);
145145
this.onModelLoaded = this.onModelLoaded.bind(this);
146146
this.onControllersUpdate = this.onControllersUpdate.bind(this);
147+
this.onControllerDisconnected = this.onControllerDisconnected.bind(this);
147148
this.checkIfControllerPresent = this.checkIfControllerPresent.bind(this);
148149
this.onAxisMoved = this.onAxisMoved.bind(this);
149150
},
@@ -155,7 +156,6 @@ module.exports.Component = registerComponent('oculus-touch-controls', {
155156
this.onButtonTouchStart = function (evt) { onButtonEvent(evt.detail.id, 'touchstart', self, self.data.hand); };
156157
this.onButtonTouchEnd = function (evt) { onButtonEvent(evt.detail.id, 'touchend', self, self.data.hand); };
157158
this.controllerPresent = false;
158-
this.lastControllerCheck = 0;
159159
this.previousButtonValues = {};
160160
this.bindMethods();
161161
this.triggerEuler = new THREE.Euler();
@@ -207,11 +207,11 @@ module.exports.Component = registerComponent('oculus-touch-controls', {
207207
loadModel: function (controller) {
208208
var data = this.data;
209209
var controllerId;
210-
211210
if (!data.model) { return; }
212211
// If model has been already loaded
213212
if (this.controllerObject3D) {
214213
this.el.setObject3D('mesh', this.controllerObject3D);
214+
this.controllerObject3D.visible = true;
215215
return;
216216
}
217217

@@ -252,10 +252,17 @@ module.exports.Component = registerComponent('oculus-touch-controls', {
252252

253253
addControllersUpdateListener: function () {
254254
this.el.sceneEl.addEventListener('controllersupdated', this.onControllersUpdate, false);
255+
this.el.addEventListener('controllerdisconnected', this.onControllerDisconnected);
255256
},
256257

257258
removeControllersUpdateListener: function () {
258259
this.el.sceneEl.removeEventListener('controllersupdated', this.onControllersUpdate, false);
260+
this.el.removeEventListener('controllerdisconnected', this.onControllerDisconnected);
261+
},
262+
263+
onControllerDisconnected: function () {
264+
if (!this.controllerObject3D) { return; }
265+
this.controllerObject3D.visible = false;
259266
},
260267

261268
onControllersUpdate: function () {

‎src/components/valve-index-controls.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ module.exports.Component = registerComponent('valve-index-controls', {
5252
init: function () {
5353
var self = this;
5454
this.controllerPresent = false;
55-
this.lastControllerCheck = 0;
5655
this.onButtonChanged = this.onButtonChanged.bind(this);
5756
this.onButtonDown = function (evt) { onButtonEvent(evt.detail.id, 'down', self); };
5857
this.onButtonUp = function (evt) { onButtonEvent(evt.detail.id, 'up', self); };

0 commit comments

Comments
 (0)