From 6e107a8f1f89dc9b1fe342358eed7576a7a8832b Mon Sep 17 00:00:00 2001
From: joni <johnfshaughnessy@gmail.com>
Date: Fri, 8 Dec 2017 12:32:23 -0800
Subject: [PATCH] Update oculus controls.

---
 src/activators/reversey.js        | 20 ++++++++++++++++
 src/activators/shortpress.js      | 39 +++++++++++++++++++++++++++++++
 src/components/haptic-feedback.js |  9 +++----
 src/input-mappings.js             | 13 +++++++----
 src/room.js                       |  3 +++
 5 files changed, 76 insertions(+), 8 deletions(-)
 create mode 100644 src/activators/reversey.js
 create mode 100644 src/activators/shortpress.js

diff --git a/src/activators/reversey.js b/src/activators/reversey.js
new file mode 100644
index 000000000..d3f3967f2
--- /dev/null
+++ b/src/activators/reversey.js
@@ -0,0 +1,20 @@
+function ReverseY(el, button, onActivate) {
+  this.el = el;
+  this.emitReverseY = this.emitReverseY.bind(this);
+  this.onActivate = onActivate;
+  this.button = button;
+  this.removeListeners = this.removeListeners.bind(this);
+  el.addEventListener(button, this.emitReverseY);
+}
+
+ReverseY.prototype = {
+  emitReverseY: function(event) {
+    event.detail.axis[1] *= -1;
+    this.onActivate(event);
+  },
+  removeListeners: function() {
+    this.el.removeEventListener(this.button, this.emitReverseY);
+  }
+};
+
+export { ReverseY };
diff --git a/src/activators/shortpress.js b/src/activators/shortpress.js
new file mode 100644
index 000000000..494de3f7a
--- /dev/null
+++ b/src/activators/shortpress.js
@@ -0,0 +1,39 @@
+function ShortPress(el, button, onActivate) {
+  this.lastTime = 0;
+  this.timeOut = 500;
+  this.eventNameDown = button + "down";
+  this.eventNameUp = button + "up";
+
+  this.el = el;
+  this.onActivate = onActivate;
+
+  this.onButtonDown = this.onButtonDown.bind(this);
+  this.onButtonUp = this.onButtonUp.bind(this);
+
+  el.addEventListener(this.eventNameDown, this.onButtonDown);
+  el.addEventListener(this.eventNameUp, this.onButtonUp);
+}
+
+ShortPress.prototype = {
+  onButtonDown(event) {
+    console.log("down");
+    var self = this;
+    this.pressTimer = window.setTimeout(function() {
+      console.log("activate");
+      self.onActivate(event);
+    }, this.timeOut);
+  },
+
+  onButtonUp(event) {
+    console.log("up");
+    clearTimeout(this.pressTimer);
+  },
+
+  removeListeners() {
+    this.el.removeEventListener(this.eventNameDown, this.onButtonDown);
+    this.el.removeEventListener(this.eventNameUp, this.onButtonUp);
+  }
+};
+
+console.log("foo");
+AFRAME.registerInputActivator("shortpress", ShortPress);
diff --git a/src/components/haptic-feedback.js b/src/components/haptic-feedback.js
index 8a58a59e1..1ef413d38 100644
--- a/src/components/haptic-feedback.js
+++ b/src/components/haptic-feedback.js
@@ -19,19 +19,20 @@ AFRAME.registerComponent("haptic-feedback", {
 
   getActuator() {
     return new Promise((resolve, reject) => {
-      setTimeout(() => {
+      const tryGetActivator = () => {
         var trackedControls = this.el.components["tracked-controls"];
         if (
           trackedControls &&
           trackedControls.controller &&
           trackedControls.controller.hapticActuators &&
-          trackedControls.controller.hapticActuators.length > 0
+          trackedControls.controller.hapticActuators.length
         ) {
           resolve(trackedControls.controller.hapticActuators[0]);
         } else {
-          return this.getActuator().then(x => resolve(x));
+          setTimeout(tryGetActivator, 1000);
         }
-      }, 1000);
+      };
+      setTimeout(tryGetActivator, 1000);
     });
   },
 
diff --git a/src/input-mappings.js b/src/input-mappings.js
index 27414e45b..106473d81 100644
--- a/src/input-mappings.js
+++ b/src/input-mappings.js
@@ -46,16 +46,21 @@ const config = {
         gripup: "middle_ring_pinky_up",
         thumbsticktouchstart: "thumb_down",
         thumbsticktouchend: "thumb_up",
-        triggerdown: "index_down",
-        triggerup: "index_up",
-        left_axismove: "move",
+        // @TODO: How do I map more than one action to triggerdown?
+        //        triggerdown: "index_down",
+        //        triggerup: "index_up",
+        triggerdown: "action_teleport_down",
+        triggerup: "action_teleport_up",
+        "axismove.reverseY": { left: "move" },
         right_dpad_east: "snap_rotate_right",
         right_dpad_west: "snap_rotate_left",
         abuttondown: "action_teleport_down",
         abuttonup: "action_teleport_up"
       },
       daydream: {
-        menudown: "action_mute"
+        menudown: "action_mute",
+        trackpaddown: "action_teleport_down",
+        trackpadup: "action_teleport_up"
       },
       keyboard: {
         m_press: "action_mute",
diff --git a/src/room.js b/src/room.js
index db02dada5..bc55230e0 100644
--- a/src/room.js
+++ b/src/room.js
@@ -13,6 +13,8 @@ AFRAME.registerComponent("animation-mixer", animationMixer);
 import { vive_trackpad_dpad4 } from "./behaviours/vive-trackpad-dpad4";
 import { oculus_touch_joystick_dpad4 } from "./behaviours/oculus-touch-joystick-dpad4";
 import { PressedMove } from "./activators/pressedmove";
+import { ReverseY } from "./activators/reversey";
+import "./activators/shortpress";
 import "./components/wasd-to-analog2d"; //Might be a behaviour or activator in the future
 
 import "./components/mute-mic";
@@ -40,6 +42,7 @@ AFRAME.registerInputBehaviour(
   oculus_touch_joystick_dpad4
 );
 AFRAME.registerInputActivator("pressedmove", PressedMove);
+AFRAME.registerInputActivator("reverseY", ReverseY);
 AFRAME.registerInputActions(inGameActions, "default");
 AFRAME.registerInputMappings(config);
 
-- 
GitLab