diff --git a/src/components/cardboard-controls.js b/src/components/cardboard-controls.js new file mode 100644 index 0000000000000000000000000000000000000000..c0d676cc5f6033cc819db0c1a69ce58928ae7dd9 --- /dev/null +++ b/src/components/cardboard-controls.js @@ -0,0 +1,34 @@ +const CARDBOARD_BUTTON = "Cardboard Button"; + +module.exports = AFRAME.registerComponent("cardboard-controls", { + init: function() { + this.buttons = {}; + this.gamepad = null; + }, + + tick: function() { + this.gamepad = this.gamepad || this._getGamepad(); + if (this.gamepad) { + for (var i = 0; i < this.gamepad.buttons.length; i++) { + if (this.gamepad.buttons[i].pressed && !this.buttons[i]) { + this.el.emit("cardboardbuttondown", {}); + } else if (!this.gamepad.buttons[i].pressed && this.buttons[i]) { + this.el.emit("cardboardbuttonup", {}); + } + this.buttons[i] = this.gamepad.buttons[i].pressed; + } + } else if (Object.keys(this.buttons)) { + this.buttons = {}; + } + }, + + _getGamepad: function() { + const gamepads = navigator.getGamepads && navigator.getGamepads(); + for (var i = 0; i < gamepads.length; i++) { + if (gamepads[i] && gamepads[i].id === CARDBOARD_BUTTON) { + return gamepads[i]; + } + } + return null; + } +}); diff --git a/src/components/cursor-controller.js b/src/components/cursor-controller.js index ba0398a6224a012d4f90bc58577791d42b6a555a..7f3b1bbc42fad4f891b1eef4aaaa7047f8156b60 100644 --- a/src/components/cursor-controller.js +++ b/src/components/cursor-controller.js @@ -93,8 +93,8 @@ AFRAME.registerComponent("cursor-controller", { this.data.playerRig.addEventListener(this.data.primaryUp, this._handlePrimaryUp); this.data.playerRig.addEventListener(this.data.grabEvent, this._handlePrimaryDown); this.data.playerRig.addEventListener(this.data.releaseEvent, this._handlePrimaryUp); - this.data.playerRig.addEventListener("gamepadbuttondown", this._handlePrimaryDown); - this.data.playerRig.addEventListener("gamepadbuttonup", this._handlePrimaryUp); + this.data.playerRig.addEventListener("cardboardbuttondown", this._handlePrimaryDown); + this.data.playerRig.addEventListener("cardboardbuttonup", this._handlePrimaryUp); this.data.playerRig.addEventListener("model-loaded", this._handleModelLoaded); @@ -118,8 +118,8 @@ AFRAME.registerComponent("cursor-controller", { this.data.playerRig.removeEventListener(this.data.primaryUp, this._handlePrimaryUp); this.data.playerRig.removeEventListener(this.data.grabEvent, this._handlePrimaryDown); this.data.playerRig.removeEventListener(this.data.releaseEvent, this._handlePrimaryUp); - this.data.playerRig.removeEventListener("gamepadbuttondown", this._handlePrimaryDown); - this.data.playerRig.removeEventListener("gamepadbuttonup", this._handlePrimaryUp); + this.data.playerRig.removeEventListener("cardboardbuttondown", this._handlePrimaryDown); + this.data.playerRig.removeEventListener("cardboardbuttonup", this._handlePrimaryUp); this.data.playerRig.removeEventListener("model-loaded", this._handleModelLoaded); @@ -386,7 +386,7 @@ AFRAME.registerComponent("cursor-controller", { }, _handlePrimaryDown: function(e) { - if (e.target === this.controller) { + if (e.target === this.controller || e.target === this.data.playerRig) { const isInteractable = this._isTargetOfType(TARGET_TYPE_INTERACTABLE) && !this.grabStarting; if (isInteractable || this._isTargetOfType(TARGET_TYPE_UI)) { this.grabStarting = true; @@ -398,7 +398,7 @@ AFRAME.registerComponent("cursor-controller", { }, _handlePrimaryUp: function(e) { - if (e.target === this.controller) { + if (e.target === this.controller || e.target === this.data.playerRig) { if (this._isGrabbing() || this._isTargetOfType(TARGET_TYPE_UI)) { this.grabStarting = false; this.data.cursor.emit("cursor-release", e.detail); diff --git a/src/hub.html b/src/hub.html index c8771f3137f4d9c1c45b71579cd911eb101e2575..799866746e990b3b79f9a1511fd05c90b9f2cefc 100644 --- a/src/hub.html +++ b/src/hub.html @@ -230,6 +230,7 @@ ik-root player-info networked-avatar + cardboard-controls > <a-entity id="player-hud" diff --git a/src/hub.js b/src/hub.js index fd8b7ad95f0d47298fb024aa703e927c45821263..2485419ff0431d554f75df1904446d2c94183b56 100644 --- a/src/hub.js +++ b/src/hub.js @@ -99,6 +99,8 @@ import "./components/controls-shape-offset"; import "./components/duck"; import "./components/quack"; +import "./components/cardboard-controls"; + import "./components/cursor-controller"; import "./components/nav-mesh-helper";