From 947afd297531aec643fec3191aa19d54ac2cfa88 Mon Sep 17 00:00:00 2001 From: Brian Peiris Date: Wed, 21 Sep 2016 18:10:57 -0700 Subject: [PATCH] Support built-in controls. Fix gamepad initialization --- README.md | 15 ++++++++++++- dist/aframe-altspace-component.js | 31 +++++++++++++++++++++++---- dist/aframe-altspace-component.min.js | 2 +- index.js | 31 +++++++++++++++++++++++---- 4 files changed, 69 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 4d72127..29756ae 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,8 @@ Note that when running in Altspace, the scene will not be synchronized between u | `verticalAlign` | Puts the scene origin at the bottom, middle, or top of the Altspace enclosure. If your scene seems to be floating in midair, try setting this to 'bottom'. | `middle` ### Usage -Add the "altspace" parameter on your `` like so: `` +Add the "altspace" attribute on your `` like so: `` #### Example @@ -34,3 +34,16 @@ Install and use by directly including the [browser files](dist): ``` + +## altspace-tracked-controls + +This library also includes an `altspace-tracked-controls` component that enables tracked control support for A-Frame +applications that use the built-in `tracked-controls`, `vive-controls` or `hand-controls` components. + +### Usage + +Add the "altspace-tracked-controls" attribute to your tracked entity. For example: + +```html + +``` diff --git a/dist/aframe-altspace-component.js b/dist/aframe-altspace-component.js index 9804934..b60ae58 100644 --- a/dist/aframe-altspace-component.js +++ b/dist/aframe-altspace-component.js @@ -208,11 +208,34 @@ }); AFRAME.registerComponent('altspace-tracked-controls', { - update: function () { - if (window.altspace && altspace.getGamepads) { + init: function () { + this.gamepadIndex = null; + this.trackedControlsSystem = document.querySelector('a-scene').systems['tracked-controls']; + this.systemGamepads = 0; + altspace.getGamepads(); + }, + tick: function () { + if ( + this.trackedControlsSystem && + this.systemGamepads !== this.trackedControlsSystem.controllers.length && + window.altspace && altspace.getGamepads && altspace.getGamepads().length + ) { + var components = this.el.components; + if (components['paint-controls']) { + this.gamepadIndex = components['paint-controls'].data.hand === 'left' ? 2 : 1; + } + if (this.gamepadIndex === null && components['hand-controls']) { + this.gamepadIndex = components['hand-controls'].data === 'left' ? 2 : 1; + } + if (this.gamepadIndex === null && components['vive-controls']) { + this.gamepadIndex = components['vive-controls'].data.hand === 'left' ? 2 : 1; + } + if (this.gamepadIndex === null && components['tracked-controls']) { + this.gamepadIndex = components['tracked-controls'].data.controller; + } + this.el.setAttribute('tracked-controls', 'id', altspace.getGamepads()[this.gamepadIndex].id); this.el.setAttribute('tracked-controls', 'controller', 0); - var gamepadIndex = this.el.components['hand-controls'].data === 'left' ? 2 : 1; - this.el.setAttribute('tracked-controls', 'id', altspace.getGamepads()[gamepadIndex].id); + this.systemGamepads = this.trackedControlsSystem.controllers.length; } } }); diff --git a/dist/aframe-altspace-component.min.js b/dist/aframe-altspace-component.min.js index 392d9b3..c11c359 100644 --- a/dist/aframe-altspace-component.min.js +++ b/dist/aframe-altspace-component.min.js @@ -1 +1 @@ -!function(e){function t(r){if(a[r])return a[r].exports;var n=a[r]={exports:{},id:r,loaded:!1};return e[r].call(n.exports,n,n.exports,t),n.loaded=!0,n.exports}var a={};return t.m=e,t.c=a,t.p="",t(0)}([function(e,t){if("undefined"==typeof AFRAME)throw new Error("Component attempted to register before AFRAME was available.");AFRAME.registerComponent("altspace",{schema:{usePixelScale:{type:"boolean","default":"false"},verticalAlign:{type:"string","default":"middle"}},init:function(){return this.el.object3D instanceof THREE.Scene?void(window.altspace&&window.altspace.inClient&&(this.el.setAttribute("vr-mode-ui",{enabled:!1}),this.initRenderer(),this.initCursorEvents())):void console.warn("aframe-altspace-component can only be attached to a-scene")},update:function(e){},remove:function(){},pause:function(){},play:function(){},initRenderer:function(){var e=this.el.object3D;this.data.usePixelScale||altspace.getEnclosure().then(function(t){e.scale.multiplyScalar(t.pixelsPerMeter)});var t=this.data.verticalAlign;"center"!==t&&altspace.getEnclosure().then(function(a){switch(t){case"bottom":e.position.y-=a.innerHeight/2;break;case"top":e.position.y+=a.innerHeight/2;break;default:console.warn("Unexpected value for verticalAlign: ",this.data.verticalAlign)}});var a=this.el.renderer=this.el.effect=altspace.getThreeJSRenderer(),r=function(){};a.setSize=r,a.setPixelRatio=r,a.setClearColor=r,a.clear=r,a.enableScissorTest=r,a.setScissor=r,a.setViewport=r,a.getPixelRatio=r,a.getMaxAnisotropy=r,a.setFaceCulling=r,a.context={canvas:{}},a.shadowMap={}},initCursorEvents:function(){var e=this.el.object3D,t=document.querySelector("a-cursor")||document.querySelector("a-entity[cursor]");t&&(t.setAttribute("material","transparent",!0),t.setAttribute("material","opacity",0));var a=function(e,a){t&&t.emit(e,{target:a}),a&&a.emit(e,{target:a})},r=null;e.addEventListener("cursordown",function(e){r=e.target,a("mousedown",e.target.el)}),e.addEventListener("cursorup",function(e){a("mouseup",e.target.el),e.target.uuid===r.uuid&&a("click",e.target.el),r=null}),e.addEventListener("cursorenter",function(e){e.target.el&&(e.target.el.addState("hovered"),t&&t.addState("hovering"),a("mouseenter",e.target.el))}),e.addEventListener("cursorleave",function(e){e.target.el&&(e.target.el.removeState("hovered"),t&&t.removeState("hovering"),a("mouseleave",e.target.el))})}}),AFRAME.registerComponent("altspace-tracked-controls",{update:function(){if(window.altspace&&altspace.getGamepads){this.el.setAttribute("tracked-controls","controller",0);var e="left"===this.el.components["hand-controls"].data?2:1;this.el.setAttribute("tracked-controls","id",altspace.getGamepads()[e].id)}}})}]); \ No newline at end of file +!function(e){function t(n){if(a[n])return a[n].exports;var r=a[n]={exports:{},id:n,loaded:!1};return e[n].call(r.exports,r,r.exports,t),r.loaded=!0,r.exports}var a={};return t.m=e,t.c=a,t.p="",t(0)}([function(e,t){if("undefined"==typeof AFRAME)throw new Error("Component attempted to register before AFRAME was available.");AFRAME.registerComponent("altspace",{schema:{usePixelScale:{type:"boolean","default":"false"},verticalAlign:{type:"string","default":"middle"}},init:function(){return this.el.object3D instanceof THREE.Scene?void(window.altspace&&window.altspace.inClient&&(this.el.setAttribute("vr-mode-ui",{enabled:!1}),this.initRenderer(),this.initCursorEvents())):void console.warn("aframe-altspace-component can only be attached to a-scene")},update:function(e){},remove:function(){},pause:function(){},play:function(){},initRenderer:function(){var e=this.el.object3D;this.data.usePixelScale||altspace.getEnclosure().then(function(t){e.scale.multiplyScalar(t.pixelsPerMeter)});var t=this.data.verticalAlign;"center"!==t&&altspace.getEnclosure().then(function(a){switch(t){case"bottom":e.position.y-=a.innerHeight/2;break;case"top":e.position.y+=a.innerHeight/2;break;default:console.warn("Unexpected value for verticalAlign: ",this.data.verticalAlign)}});var a=this.el.renderer=this.el.effect=altspace.getThreeJSRenderer(),n=function(){};a.setSize=n,a.setPixelRatio=n,a.setClearColor=n,a.clear=n,a.enableScissorTest=n,a.setScissor=n,a.setViewport=n,a.getPixelRatio=n,a.getMaxAnisotropy=n,a.setFaceCulling=n,a.context={canvas:{}},a.shadowMap={}},initCursorEvents:function(){var e=this.el.object3D,t=document.querySelector("a-cursor")||document.querySelector("a-entity[cursor]");t&&(t.setAttribute("material","transparent",!0),t.setAttribute("material","opacity",0));var a=function(e,a){t&&t.emit(e,{target:a}),a&&a.emit(e,{target:a})},n=null;e.addEventListener("cursordown",function(e){n=e.target,a("mousedown",e.target.el)}),e.addEventListener("cursorup",function(e){a("mouseup",e.target.el),e.target.uuid===n.uuid&&a("click",e.target.el),n=null}),e.addEventListener("cursorenter",function(e){e.target.el&&(e.target.el.addState("hovered"),t&&t.addState("hovering"),a("mouseenter",e.target.el))}),e.addEventListener("cursorleave",function(e){e.target.el&&(e.target.el.removeState("hovered"),t&&t.removeState("hovering"),a("mouseleave",e.target.el))})}}),AFRAME.registerComponent("altspace-tracked-controls",{init:function(){this.gamepadIndex=null,this.trackedControlsSystem=document.querySelector("a-scene").systems["tracked-controls"],this.systemGamepads=0,altspace.getGamepads()},tick:function(){if(this.trackedControlsSystem&&this.systemGamepads!==this.trackedControlsSystem.controllers.length&&window.altspace&&altspace.getGamepads&&altspace.getGamepads().length){var e=this.el.components;e["paint-controls"]&&(this.gamepadIndex="left"===e["paint-controls"].data.hand?2:1),null===this.gamepadIndex&&e["hand-controls"]&&(this.gamepadIndex="left"===e["hand-controls"].data?2:1),null===this.gamepadIndex&&e["vive-controls"]&&(this.gamepadIndex="left"===e["vive-controls"].data.hand?2:1),null===this.gamepadIndex&&e["tracked-controls"]&&(this.gamepadIndex=e["tracked-controls"].data.controller),this.el.setAttribute("tracked-controls","id",altspace.getGamepads()[this.gamepadIndex].id),this.el.setAttribute("tracked-controls","controller",0),this.systemGamepads=this.trackedControlsSystem.controllers.length}}})}]); \ No newline at end of file diff --git a/index.js b/index.js index bff94e1..a83cc64 100644 --- a/index.js +++ b/index.js @@ -162,11 +162,34 @@ AFRAME.registerComponent('altspace', { }); AFRAME.registerComponent('altspace-tracked-controls', { - update: function () { - if (window.altspace && altspace.getGamepads) { + init: function () { + this.gamepadIndex = null; + this.trackedControlsSystem = document.querySelector('a-scene').systems['tracked-controls']; + this.systemGamepads = 0; + altspace.getGamepads(); + }, + tick: function () { + if ( + this.trackedControlsSystem && + this.systemGamepads !== this.trackedControlsSystem.controllers.length && + window.altspace && altspace.getGamepads && altspace.getGamepads().length + ) { + var components = this.el.components; + if (components['paint-controls']) { + this.gamepadIndex = components['paint-controls'].data.hand === 'left' ? 2 : 1; + } + if (this.gamepadIndex === null && components['hand-controls']) { + this.gamepadIndex = components['hand-controls'].data === 'left' ? 2 : 1; + } + if (this.gamepadIndex === null && components['vive-controls']) { + this.gamepadIndex = components['vive-controls'].data.hand === 'left' ? 2 : 1; + } + if (this.gamepadIndex === null && components['tracked-controls']) { + this.gamepadIndex = components['tracked-controls'].data.controller; + } + this.el.setAttribute('tracked-controls', 'id', altspace.getGamepads()[this.gamepadIndex].id); this.el.setAttribute('tracked-controls', 'controller', 0); - var gamepadIndex = this.el.components['hand-controls'].data === 'left' ? 2 : 1; - this.el.setAttribute('tracked-controls', 'id', altspace.getGamepads()[gamepadIndex].id); + this.systemGamepads = this.trackedControlsSystem.controllers.length; } } });