From 202b758975c74c0c3cf65dceacbefea8c5033136 Mon Sep 17 00:00:00 2001 From: dmarcos Date: Wed, 5 Apr 2017 19:52:26 -0700 Subject: [PATCH] Reverts PR #2314 --- src/components/oculus-touch-controls.js | 47 +++++++++++++++---- src/components/vive-controls.js | 8 +++- .../components/oculus-touch-controls.test.js | 18 +++++++ tests/components/vive-controls.test.js | 14 ++++++ 4 files changed, 77 insertions(+), 10 deletions(-) diff --git a/src/components/oculus-touch-controls.js b/src/components/oculus-touch-controls.js index fae9944f362..d8a25b731b5 100644 --- a/src/components/oculus-touch-controls.js +++ b/src/components/oculus-touch-controls.js @@ -65,6 +65,9 @@ module.exports.Component = registerComponent('oculus-touch-controls', { this.onModelLoaded = bind(this.onModelLoaded, this); this.onControllersUpdate = bind(this.onControllersUpdate, this); this.checkIfControllerPresent = bind(this.checkIfControllerPresent, this); + this.removeControllersUpdateListener = bind(this.removeControllersUpdateListener, this); + this.onGamepadConnected = bind(this.onGamepadConnected, this); + this.onGamepadDisconnected = bind(this.onGamepadDisconnected, this); }, init: function () { @@ -91,9 +94,6 @@ module.exports.Component = registerComponent('oculus-touch-controls', { el.addEventListener('touchstart', this.onButtonTouchStart); el.addEventListener('touchend', this.onButtonTouchEnd); el.addEventListener('model-loaded', this.onModelLoaded); - el.sceneEl.addEventListener('controllersupdated', this.onControllersUpdate, false); - window.addEventListener('gamepadconnected', this.checkIfControllerPresent, false); - window.addEventListener('gamepaddisconnected', this.checkIfControllerPresent, false); }, removeEventListeners: function () { @@ -104,9 +104,6 @@ module.exports.Component = registerComponent('oculus-touch-controls', { el.removeEventListener('touchstart', this.onButtonTouchStart); el.removeEventListener('touchend', this.onButtonTouchEnd); el.removeEventListener('model-loaded', this.onModelLoaded); - el.sceneEl.removeEventListener('controllersupdated', this.onControllersUpdate, false); - window.removeEventListener('gamepadconnected', this.checkIfControllerPresent, false); - window.removeEventListener('gamepaddisconnected', this.checkIfControllerPresent, false); }, checkIfControllerPresent: function () { @@ -114,15 +111,41 @@ module.exports.Component = registerComponent('oculus-touch-controls', { var isPresent = this.isControllerPresent(this.el.sceneEl, GAMEPAD_ID_PREFIX, { hand: data.hand }); if (isPresent === this.controllerPresent) { return; } this.controllerPresent = isPresent; - if (isPresent) { this.injectTrackedControls(); } // inject track-controls + if (isPresent) { + this.injectTrackedControls(); // inject track-controls + this.addEventListeners(); + } else { + this.removeEventListeners(); + } + }, + + onGamepadConnected: function (evt) { + // for now, don't disable controller update listening, due to + // apparent issue with FF Nightly only sending one event and seeing one controller; + // this.everGotGamepadEvent = true; + // this.removeControllersUpdateListener(); + this.checkIfControllerPresent(); + }, + + onGamepadDisconnected: function (evt) { + // for now, don't disable controller update listening, due to + // apparent issue with FF Nightly only sending one event and seeing one controller; + // this.everGotGamepadEvent = true; + // this.removeControllersUpdateListener(); + this.checkIfControllerPresent(); }, play: function () { this.checkIfControllerPresent(); - this.addEventListeners(); + window.addEventListener('gamepadconnected', this.onGamepadConnected, false); + window.addEventListener('gamepaddisconnected', this.onGamepadDisconnected, false); + this.addControllersUpdateListener(); }, pause: function () { + window.removeEventListener('gamepadconnected', this.onGamepadConnected, false); + window.removeEventListener('gamepaddisconnected', this.onGamepadDisconnected, false); + this.removeControllersUpdateListener(); this.removeEventListeners(); }, @@ -153,6 +176,14 @@ module.exports.Component = registerComponent('oculus-touch-controls', { this.updateControllerModel(); }, + addControllersUpdateListener: function () { + this.el.sceneEl.addEventListener('controllersupdated', this.onControllersUpdate, false); + }, + + removeControllersUpdateListener: function () { + this.el.sceneEl.removeEventListener('controllersupdated', this.onControllersUpdate, false); + }, + onControllersUpdate: function () { if (!this.everGotGamepadEvent) { this.checkIfControllerPresent(); } }, diff --git a/src/components/vive-controls.js b/src/components/vive-controls.js index 2c58e9a5379..71eabcdd802 100644 --- a/src/components/vive-controls.js +++ b/src/components/vive-controls.js @@ -91,7 +91,12 @@ module.exports.Component = registerComponent('vive-controls', { var isPresent = this.isControllerPresent(this.el.sceneEl, GAMEPAD_ID_PREFIX, { index: controller }); if (isPresent === this.controllerPresent) { return; } this.controllerPresent = isPresent; - if (isPresent) { this.injectTrackedControls(); } // inject track-controls + if (isPresent) { + this.injectTrackedControls(); // inject track-controls + this.addEventListeners(); + } else { + this.removeEventListeners(); + } }, onGamepadConnected: function (evt) { @@ -115,7 +120,6 @@ module.exports.Component = registerComponent('vive-controls', { window.addEventListener('gamepadconnected', this.onGamepadConnected, false); window.addEventListener('gamepaddisconnected', this.onGamepadDisconnected, false); this.addControllersUpdateListener(); - this.addEventListeners(); }, pause: function () { diff --git a/tests/components/oculus-touch-controls.test.js b/tests/components/oculus-touch-controls.test.js index 6ecb60d9592..9a056dbdb82 100644 --- a/tests/components/oculus-touch-controls.test.js +++ b/tests/components/oculus-touch-controls.test.js @@ -17,7 +17,9 @@ suite(controllerComponentName, function () { test('first-time, if no controllers, remove event listeners and remember not present', function () { var el = this.el; var controllerComponent = el.components[controllerComponentName]; + var addEventListenersSpy = this.sinon.spy(controllerComponent, 'addEventListeners'); var injectTrackedControlsSpy = this.sinon.spy(controllerComponent, 'injectTrackedControls'); + var removeEventListenersSpy = this.sinon.spy(controllerComponent, 'removeEventListeners'); // mock isControllerPresent to return false controllerComponent.isControllerPresentMockValue = false; // reset so we don't think we've looked before @@ -26,6 +28,8 @@ suite(controllerComponentName, function () { controllerComponent.checkIfControllerPresent(); // check assertions assert.notOk(injectTrackedControlsSpy.called); + assert.notOk(addEventListenersSpy.called); + assert.ok(removeEventListenersSpy.called); assert.ok(controllerComponent.controllerPresent === false); // not undefined }); @@ -34,6 +38,7 @@ suite(controllerComponentName, function () { var controllerComponent = el.components[controllerComponentName]; var addEventListenersSpy = this.sinon.spy(controllerComponent, 'addEventListeners'); var injectTrackedControlsSpy = this.sinon.spy(controllerComponent, 'injectTrackedControls'); + var removeEventListenersSpy = this.sinon.spy(controllerComponent, 'removeEventListeners'); // mock isControllerPresent to return false controllerComponent.isControllerPresentMockValue = false; // pretend we've looked before @@ -43,13 +48,16 @@ suite(controllerComponentName, function () { // check assertions assert.notOk(injectTrackedControlsSpy.called); assert.notOk(addEventListenersSpy.called); + assert.notOk(removeEventListenersSpy.called); assert.ok(controllerComponent.controllerPresent === false); // not undefined }); test('attach events if controller is newly present', function () { var el = this.el; var controllerComponent = el.components[controllerComponentName]; + var addEventListenersSpy = this.sinon.spy(controllerComponent, 'addEventListeners'); var injectTrackedControlsSpy = this.sinon.spy(controllerComponent, 'injectTrackedControls'); + var removeEventListenersSpy = this.sinon.spy(controllerComponent, 'removeEventListeners'); // mock isControllerPresent to return true controllerComponent.isControllerPresentMockValue = true; // reset so we don't think we've looked before @@ -58,13 +66,17 @@ suite(controllerComponentName, function () { controllerComponent.checkIfControllerPresent(); // check assertions assert.ok(injectTrackedControlsSpy.called); + assert.ok(addEventListenersSpy.called); + assert.notOk(removeEventListenersSpy.called); assert.ok(controllerComponent.controllerPresent); }); test('do not inject or attach events again if controller is already present', function () { var el = this.el; var controllerComponent = el.components[controllerComponentName]; + var addEventListenersSpy = this.sinon.spy(controllerComponent, 'addEventListeners'); var injectTrackedControlsSpy = this.sinon.spy(controllerComponent, 'injectTrackedControls'); + var removeEventListenersSpy = this.sinon.spy(controllerComponent, 'removeEventListeners'); // mock isControllerPresent to return true controllerComponent.isControllerPresentMockValue = true; // pretend we've looked before @@ -73,13 +85,17 @@ suite(controllerComponentName, function () { controllerComponent.checkIfControllerPresent(); // check assertions assert.notOk(injectTrackedControlsSpy.called); + assert.notOk(addEventListenersSpy.called); + assert.notOk(removeEventListenersSpy.called); assert.ok(controllerComponent.controllerPresent); }); test('if controller disappears, remove event listeners', function () { var el = this.el; var controllerComponent = el.components[controllerComponentName]; + var addEventListenersSpy = this.sinon.spy(controllerComponent, 'addEventListeners'); var injectTrackedControlsSpy = this.sinon.spy(controllerComponent, 'injectTrackedControls'); + var removeEventListenersSpy = this.sinon.spy(controllerComponent, 'removeEventListeners'); // mock isControllerPresent to return true controllerComponent.isControllerPresentMockValue = false; // pretend we've looked before @@ -88,6 +104,8 @@ suite(controllerComponentName, function () { controllerComponent.checkIfControllerPresent(); // check assertions assert.notOk(injectTrackedControlsSpy.called); + assert.notOk(addEventListenersSpy.called); + assert.ok(removeEventListenersSpy.called); assert.notOk(controllerComponent.controllerPresent); }); }); diff --git a/tests/components/vive-controls.test.js b/tests/components/vive-controls.test.js index 13e57a5b7bb..fc50a126635 100644 --- a/tests/components/vive-controls.test.js +++ b/tests/components/vive-controls.test.js @@ -17,7 +17,9 @@ suite(controllerComponentName, function () { test('first-time, if no controllers, remove event listeners and remember not present', function () { var el = this.el; var controllerComponent = el.components[controllerComponentName]; + var addEventListenersSpy = this.sinon.spy(controllerComponent, 'addEventListeners'); var injectTrackedControlsSpy = this.sinon.spy(controllerComponent, 'injectTrackedControls'); + var removeEventListenersSpy = this.sinon.spy(controllerComponent, 'removeEventListeners'); // mock isControllerPresent to return false controllerComponent.isControllerPresentMockValue = false; // reset so we don't think we've looked before @@ -26,6 +28,8 @@ suite(controllerComponentName, function () { controllerComponent.checkIfControllerPresent(); // check assertions assert.notOk(injectTrackedControlsSpy.called); + assert.notOk(addEventListenersSpy.called); + assert.ok(removeEventListenersSpy.called); assert.ok(controllerComponent.controllerPresent === false); // not undefined }); @@ -51,7 +55,9 @@ suite(controllerComponentName, function () { test('attach events if controller is newly present', function () { var el = this.el; var controllerComponent = el.components[controllerComponentName]; + var addEventListenersSpy = this.sinon.spy(controllerComponent, 'addEventListeners'); var injectTrackedControlsSpy = this.sinon.spy(controllerComponent, 'injectTrackedControls'); + var removeEventListenersSpy = this.sinon.spy(controllerComponent, 'removeEventListeners'); // mock isControllerPresent to return true controllerComponent.isControllerPresentMockValue = true; // reset so we don't think we've looked before @@ -60,6 +66,8 @@ suite(controllerComponentName, function () { controllerComponent.checkIfControllerPresent(); // check assertions assert.ok(injectTrackedControlsSpy.called); + assert.ok(addEventListenersSpy.called); + assert.notOk(removeEventListenersSpy.called); assert.ok(controllerComponent.controllerPresent); }); @@ -68,6 +76,7 @@ suite(controllerComponentName, function () { var controllerComponent = el.components[controllerComponentName]; var addEventListenersSpy = this.sinon.spy(controllerComponent, 'addEventListeners'); var injectTrackedControlsSpy = this.sinon.spy(controllerComponent, 'injectTrackedControls'); + var removeEventListenersSpy = this.sinon.spy(controllerComponent, 'removeEventListeners'); // mock isControllerPresent to return true controllerComponent.isControllerPresentMockValue = true; // pretend we've looked before @@ -77,13 +86,16 @@ suite(controllerComponentName, function () { // check assertions assert.notOk(injectTrackedControlsSpy.called); assert.notOk(addEventListenersSpy.called); + assert.notOk(removeEventListenersSpy.called); assert.ok(controllerComponent.controllerPresent); }); test('if controller disappears, remove event listeners', function () { var el = this.el; var controllerComponent = el.components[controllerComponentName]; + var addEventListenersSpy = this.sinon.spy(controllerComponent, 'addEventListeners'); var injectTrackedControlsSpy = this.sinon.spy(controllerComponent, 'injectTrackedControls'); + var removeEventListenersSpy = this.sinon.spy(controllerComponent, 'removeEventListeners'); // mock isControllerPresent to return true controllerComponent.isControllerPresentMockValue = false; // pretend we've looked before @@ -92,6 +104,8 @@ suite(controllerComponentName, function () { controllerComponent.checkIfControllerPresent(); // check assertions assert.notOk(injectTrackedControlsSpy.called); + assert.notOk(addEventListenersSpy.called); + assert.ok(removeEventListenersSpy.called); assert.notOk(controllerComponent.controllerPresent); }); });