Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revert PR #2314 (Attaches event listeners to the controllers controls regardless of presence) #2556

Merged
merged 1 commit into from
Apr 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 39 additions & 8 deletions src/components/oculus-touch-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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 () {
Expand All @@ -104,25 +104,48 @@ 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 () {
var data = this.data;
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();
},

Expand Down Expand Up @@ -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(); }
},
Expand Down
8 changes: 6 additions & 2 deletions src/components/vive-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 () {
Expand Down
18 changes: 18 additions & 0 deletions tests/components/oculus-touch-controls.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
});

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);
});
});
Expand Down
14 changes: 14 additions & 0 deletions tests/components/vive-controls.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
});

Expand All @@ -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
Expand All @@ -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);
});

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);
});
});
Expand Down