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

Emit event when oculus or vive controllers are connected #2505

Merged
merged 3 commits into from
Apr 22, 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
14 changes: 3 additions & 11 deletions src/components/daydream-controls.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var registerComponent = require('../core/component').registerComponent;
var bind = require('../utils/bind');
var isControllerPresent = require('../utils/tracked-controls').isControllerPresent;
var checkControllerPresentAndSetup = require('../utils/tracked-controls').checkControllerPresentAndSetup;
var emitIfAxesChanged = require('../utils/tracked-controls').emitIfAxesChanged;

var DAYDREAM_CONTROLLER_MODEL_BASE_URL = 'https://cdn.aframe.io/controllers/google/';
Expand Down Expand Up @@ -57,7 +57,7 @@ module.exports.Component = registerComponent('daydream-controls', {
this.everGotGamepadEvent = false;
this.lastControllerCheck = 0;
this.bindMethods();
this.isControllerPresent = isControllerPresent; // to allow mock
this.checkControllerPresentAndSetup = checkControllerPresentAndSetup; // to allow mock
this.emitIfAxesChanged = emitIfAxesChanged; // to allow mock
},

Expand All @@ -82,15 +82,7 @@ module.exports.Component = registerComponent('daydream-controls', {
},

checkIfControllerPresent: function () {
var isPresent = this.isControllerPresent(this.el.sceneEl, GAMEPAD_ID_PREFIX, {hand: this.data.hand});
if (isPresent === this.controllerPresent) { return; }
this.controllerPresent = isPresent;
if (isPresent) {
this.addEventListeners();
this.injectTrackedControls();
} else {
this.removeEventListeners();
}
this.checkControllerPresentAndSetup(this, GAMEPAD_ID_PREFIX, {hand: this.data.hand});
},

onGamepadConnectionEvent: function (evt) {
Expand Down
22 changes: 9 additions & 13 deletions src/components/gearvr-controls.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var registerComponent = require('../core/component').registerComponent;
var bind = require('../utils/bind');
var isControllerPresent = require('../utils/tracked-controls').isControllerPresent;
var checkControllerPresentAndSetup = require('../utils/tracked-controls').checkControllerPresentAndSetup;
var emitIfAxesChanged = require('../utils/tracked-controls').emitIfAxesChanged;

var GEARVR_CONTROLLER_MODEL_BASE_URL = 'https://cdn.aframe.io/controllers/samsung/';
Expand Down Expand Up @@ -58,7 +58,7 @@ module.exports.Component = registerComponent('gearvr-controls', {
this.everGotGamepadEvent = false;
this.lastControllerCheck = 0;
this.bindMethods();
this.isControllerPresent = isControllerPresent; // to allow mock
this.checkControllerPresentAndSetup = checkControllerPresentAndSetup; // to allow mock
this.emitIfAxesChanged = emitIfAxesChanged; // to allow mock
},

Expand All @@ -70,6 +70,8 @@ module.exports.Component = registerComponent('gearvr-controls', {
el.addEventListener('touchend', this.onButtonTouchEnd);
el.addEventListener('model-loaded', this.onModelLoaded);
el.addEventListener('axismove', this.onAxisMoved);

this.addControllersUpdateListener();
},

removeEventListeners: function () {
Expand All @@ -80,18 +82,12 @@ module.exports.Component = registerComponent('gearvr-controls', {
el.removeEventListener('touchend', this.onButtonTouchEnd);
el.removeEventListener('model-loaded', this.onModelLoaded);
el.removeEventListener('axismove', this.onAxisMoved);

this.removeControllersUpdateListener();
},

checkIfControllerPresent: function () {
var isPresent = this.isControllerPresent(this.el.sceneEl, GAMEPAD_ID_PREFIX, this.data.hand ? {hand: this.data.hand} : {});
if (isPresent === this.controllerPresent) { return; }
this.controllerPresent = isPresent;
if (isPresent) {
this.injectTrackedControls();
this.addEventListeners();
} else {
this.removeEventListeners();
}
this.checkControllerPresentAndSetup(this, GAMEPAD_ID_PREFIX, this.data.hand ? {hand: this.data.hand} : {});
},

onGamepadConnectionEvent: function (evt) {
Expand All @@ -106,10 +102,10 @@ module.exports.Component = registerComponent('gearvr-controls', {
},

pause: function () {
this.removeEventListeners();
this.removeControllersUpdateListener();
// Note that due to gamepadconnected event propagation issues, we don't rely on events.
window.removeEventListener('gamepaddisconnected', this.checkIfControllerPresent, false);
this.removeControllersUpdateListener();
this.removeEventListeners();
},

injectTrackedControls: function () {
Expand Down
23 changes: 3 additions & 20 deletions src/components/oculus-touch-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module.exports.Component = registerComponent('oculus-touch-controls', {
this.bindMethods();

this.emitIfAxesChanged = controllerUtils.emitIfAxesChanged; // Allow mock.
this.isControllerPresent = controllerUtils.isControllerPresent; // Allow mock.
this.checkControllerPresentAndSetup = controllerUtils.checkControllerPresentAndSetup; // Allow mock.
},

addEventListeners: function () {
Expand All @@ -96,25 +96,9 @@ module.exports.Component = registerComponent('oculus-touch-controls', {
},

checkIfControllerPresent: function () {
var data = this.data;
var isPresent;

// Find which controller matches both prefix and hand.
isPresent = this.isControllerPresent(this.el.sceneEl, GAMEPAD_ID_PREFIX, {
hand: data.hand
this.checkControllerPresentAndSetup(this, GAMEPAD_ID_PREFIX, {
hand: this.data.hand
});

// Nothing changed, no need to do anything.
if (isPresent === this.controllerPresent) { return; }

// Update controller presence.
this.controllerPresent = isPresent;
if (isPresent) {
this.injectTrackedControls();
this.addEventListeners();
} else {
this.removeEventListeners();
}
},

play: function () {
Expand All @@ -125,7 +109,6 @@ module.exports.Component = registerComponent('oculus-touch-controls', {
},

pause: function () {
this.removeControllersUpdateListener();
this.removeEventListeners();
this.removeControllersUpdateListener();
// Note that due to gamepadconnected event propagation issues, we don't rely on events.
Expand Down
16 changes: 4 additions & 12 deletions src/components/vive-controls.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var registerComponent = require('../core/component').registerComponent;
var bind = require('../utils/bind');
var isControllerPresent = require('../utils/tracked-controls').isControllerPresent;
var checkControllerPresentAndSetup = require('../utils/tracked-controls').checkControllerPresentAndSetup;
var emitIfAxesChanged = require('../utils/tracked-controls').emitIfAxesChanged;

var VIVE_CONTROLLER_MODEL_OBJ_URL = 'https://cdn.aframe.io/controllers/vive/vr_controller_vive.obj';
Expand Down Expand Up @@ -59,7 +59,7 @@ module.exports.Component = registerComponent('vive-controls', {
this.lastControllerCheck = 0;
this.previousButtonValues = {};
this.bindMethods();
this.isControllerPresent = isControllerPresent; // to allow mock
this.checkControllerPresentAndSetup = checkControllerPresentAndSetup; // to allow mock
this.emitIfAxesChanged = emitIfAxesChanged; // to allow mock
},

Expand Down Expand Up @@ -88,18 +88,10 @@ module.exports.Component = registerComponent('vive-controls', {
checkIfControllerPresent: function () {
var data = this.data;
// Once OpenVR / SteamVR return correct hand data in the supporting browsers, we can use hand property.
// var isPresent = this.isControllerPresent(this.el.sceneEl, GAMEPAD_ID_PREFIX, { hand: data.hand });
// var isPresent = this.checkControllerPresentAndSetup(this.el.sceneEl, GAMEPAD_ID_PREFIX, { hand: data.hand });
// Until then, use hardcoded index.
var controllerIndex = data.hand === 'right' ? 0 : data.hand === 'left' ? 1 : 2;
var isPresent = this.isControllerPresent(this.el.sceneEl, GAMEPAD_ID_PREFIX, { index: controllerIndex });
if (isPresent === this.controllerPresent) { return; }
this.controllerPresent = isPresent;
if (isPresent) {
this.injectTrackedControls();
this.addEventListeners();
} else {
this.removeEventListeners();
}
this.checkControllerPresentAndSetup(this, GAMEPAD_ID_PREFIX, { index: controllerIndex });
},

play: function () {
Expand Down
40 changes: 37 additions & 3 deletions src/utils/tracked-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,57 @@ module.exports.getGamepadsByPrefix = function (idPrefix) {
return gamepadsList;
};

/**
* Check if the controller match the parameters and inject the tracked-controls component
* and add eventlistener, otherwise it will just remove the listener.
* It will also generate a controllerconnected or controllerdisconnected.
*
* @param {object} component - the tracked controls component.
* @param {object} idPrefix - prefix to match in gamepad id, if any.
* @param {object} queryObject - map of values to match (hand; index among controllers with idPrefix)
*/
module.exports.checkControllerPresentAndSetup = function (component, idPrefix, queryObject) {
var el = component.el;
var isPresent = isControllerPresent(component, idPrefix, queryObject);

// Nothing changed, no need to do anything.
if (isPresent === component.controllerPresent) { return isPresent; }
component.controllerPresent = isPresent;

// Update controller presence.
if (isPresent) {
component.injectTrackedControls();
component.addEventListeners();
el.emit('controllerconnected', {name: component.name, component: component});
} else {
component.removeEventListeners();
el.emit('controllerdisconnected', {name: component.name, component: component});
}
};

/**
* Enumerate controllers (as built by system tick, e.g. that have pose) and check if they match parameters.
*
* @param {object} sceneEl - the scene element.
* @param {object} component - the tracked controls component.
* @param {object} idPrefix - prefix to match in gamepad id, if any.
* @param {object} queryObject - map of values to match (hand; index among controllers with idPrefix)
*/
module.exports.isControllerPresent = function (sceneEl, idPrefix, queryObject) {
function isControllerPresent (component, idPrefix, queryObject) {
var isPresent = false;
var index = 0;
var gamepad;
var isPrefixMatch;
var gamepads;
var sceneEl = component.el.sceneEl;

var trackedControlsSystem = sceneEl && sceneEl.systems['tracked-controls'];
if (!trackedControlsSystem) { return isPresent; }
gamepads = trackedControlsSystem.controllers;
if (!gamepads || gamepads.length === 0) {
trackedControlsSystem.updateControllerList();
gamepads = trackedControlsSystem.controllers;
}

if (!gamepads) { return isPresent; }

for (var i = 0; i < gamepads.length; ++i) {
Expand All @@ -59,8 +90,11 @@ module.exports.isControllerPresent = function (sceneEl, idPrefix, queryObject) {
if (isPresent) { break; }
if (isPrefixMatch) { index++; } // update count of gamepads with idPrefix
}

return isPresent;
};
}

module.exports.isControllerPresent = isControllerPresent;

/**
* Emit specific moved event(s) if axes changed, based on original axismoved event.
Expand Down
22 changes: 6 additions & 16 deletions tests/components/oculus-touch-controls.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ suite('oculus-touch-controls', function () {
el.setAttribute('oculus-touch-controls', '');
el.addEventListener('loaded', function () {
component = el.components['oculus-touch-controls'];
component.controllersWhenPresent = [{id: 'Oculus Touch', index: 0, hand: 'left', pose: {}}];
done();
});
});
Expand All @@ -20,9 +21,6 @@ suite('oculus-touch-controls', function () {
var injectTrackedControlsSpy = this.sinon.spy(component, 'injectTrackedControls');
var removeEventListenersSpy = this.sinon.spy(component, 'removeEventListeners');

// Mock isControllerPresent to return false.
this.sinon.stub(component, 'isControllerPresent', () => false);

// Mock has not been checked previously.
delete component.controllerPresent;

Expand All @@ -39,9 +37,6 @@ suite('oculus-touch-controls', function () {
var injectTrackedControlsSpy = this.sinon.spy(component, 'injectTrackedControls');
var removeEventListenersSpy = this.sinon.spy(component, 'removeEventListeners');

// Mock isControllerPresent to return false.
this.sinon.stub(component, 'isControllerPresent', () => false);

// Mock that it's been checked previously.
component.controllerPresent = false;

Expand All @@ -59,7 +54,7 @@ suite('oculus-touch-controls', function () {
var removeEventListenersSpy = this.sinon.spy(component, 'removeEventListeners');

// Mock isControllerPresent to return true.
this.sinon.stub(component, 'isControllerPresent', () => true);
el.sceneEl.systems['tracked-controls'].controllers = component.controllersWhenPresent;

// Mock that it's never been checked previously.
delete component.controllerPresent;
Expand All @@ -78,7 +73,7 @@ suite('oculus-touch-controls', function () {
var removeEventListenersSpy = this.sinon.spy(component, 'removeEventListeners');

// Mock isControllerPresent to return true.
this.sinon.stub(component, 'isControllerPresent', () => true);
el.sceneEl.systems['tracked-controls'].controllers = component.controllersWhenPresent;

// Mock that it's was currently present.
component.controllerPresent = true;
Expand All @@ -96,7 +91,6 @@ suite('oculus-touch-controls', function () {
var injectTrackedControlsSpy = this.sinon.spy(component, 'injectTrackedControls');

// Mock isControllerPresent to return true.
this.sinon.stub(component, 'isControllerPresent', () => false);

// Mock that it's was currently present.
component.controllerPresent = true;
Expand All @@ -112,8 +106,7 @@ suite('oculus-touch-controls', function () {
suite('axismove', function () {
test('can emit thumbstick moved', function (done) {
var evt;
// Mock isControllerPresent to return true.
this.sinon.stub(component, 'isControllerPresent', () => true);
el.sceneEl.systems['tracked-controls'].controllers = component.controllersWhenPresent;
// Do the check.
component.checkIfControllerPresent();
// Install event handler listening for thumbstickmoved.
Expand All @@ -130,8 +123,7 @@ suite('oculus-touch-controls', function () {

test('does not emit thumbstickmoved if axismove has no changes', function (done) {
var evt;
// Mock isControllerPresent to return true.
this.sinon.stub(component, 'isControllerPresent', () => true);
el.sceneEl.systems['tracked-controls'].controllers = component.controllersWhenPresent;
// Do the check.
component.checkIfControllerPresent();
// Install event handler listening for thumbstickmoved.
Expand All @@ -148,8 +140,7 @@ suite('oculus-touch-controls', function () {
suite('buttonchanged', function () {
test('can emit triggerchanged', function (done) {
var evt;
// Mock isControllerPresent to return true.
this.sinon.stub(component, 'isControllerPresent', () => true);
el.sceneEl.systems['tracked-controls'].controllers = component.controllersWhenPresent;
// Do the check.
component.checkIfControllerPresent();
// Install event handler listening for triggerchanged.
Expand Down Expand Up @@ -178,7 +169,6 @@ suite('oculus-touch-controls', function () {
component.pause();
component.play();
// Mock isControllerPresent to return false.
this.sinon.stub(component, 'isControllerPresent', () => false);
// Reset everGotGamepadEvent so we don't think we've looked before.
delete component.everGotGamepadEvent;
// Fire emulated gamepaddisconnected event.
Expand Down
Loading