From 28e7b230fa7f8a7dbe443e73aa98c350f2dddd70 Mon Sep 17 00:00:00 2001
From: Kevin Lee <kevin@infinite-lee.com>
Date: Wed, 9 May 2018 17:30:34 -0700
Subject: [PATCH] added carboard-controls component and hooked it up to
 cursor-controller

---
 src/components/cardboard-controls.js | 34 ++++++++++++++++++++++++++++
 src/components/cursor-controller.js  | 12 +++++-----
 src/hub.html                         |  1 +
 src/hub.js                           |  2 ++
 4 files changed, 43 insertions(+), 6 deletions(-)
 create mode 100644 src/components/cardboard-controls.js

diff --git a/src/components/cardboard-controls.js b/src/components/cardboard-controls.js
new file mode 100644
index 000000000..c0d676cc5
--- /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 ba0398a62..7f3b1bbc4 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 c8771f313..799866746 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 fd8b7ad95..2485419ff 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";
-- 
GitLab