From f0e58fea9ae14f9fd7f7b21b7dd5023ef8b5ebcf Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Fri, 26 May 2017 11:42:43 -0700 Subject: [PATCH 01/11] Modify gracefulControls SPACE key to be toggle --- script-archive/gracefulControls.js | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/script-archive/gracefulControls.js b/script-archive/gracefulControls.js index ab5de48ae2..3179044c2c 100644 --- a/script-archive/gracefulControls.js +++ b/script-archive/gracefulControls.js @@ -45,8 +45,9 @@ var KEY_LEFT = "a"; var KEY_RIGHT = "d"; var KEY_UP = "e"; var KEY_DOWN = "c"; -var KEY_ENABLE = "SPACE"; -var CAPTURED_KEYS = [KEY_BRAKE, KEY_FORWARD, KEY_BACKWARD, KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN, KEY_ENABLE]; +var KEY_TOGGLE= "SPACE"; +var KEY_DISABLE = "ESC"; +var CAPTURED_KEYS = [KEY_BRAKE, KEY_FORWARD, KEY_BACKWARD, KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN, KEY_TOGGLE]; // Global Variables var keys = {}; @@ -63,12 +64,10 @@ var yawSpeed = 0; var pitchSpeed = 0; function keyPressEvent(event) { - if (event.text == "ESC") { + if (event.text == KEY_DISABLE) { disable(); - } else if (event.text == KEY_ENABLE) { - if (Window.hasFocus()) { - enable(); - } + } else if (event.text == KEY_TOGGLE) { + toggleEnabled(); } else if (event.text == KEY_BRAKE) { movementParameters = BRAKE_PARAMETERS; } @@ -178,8 +177,17 @@ function resetCursorPosition() { lastY = newY; } + +function toggleEnabled() { + if (enabled) { + disable(); + } else { + enable(); + } +} + function enable() { - if (!enabled) { + if (!enabled && Window.hasFocus()) { enabled = true; resetCursorPosition(); From 73d6da9f4627b351e5d74838d09ba07a002a904b Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Fri, 26 May 2017 11:43:05 -0700 Subject: [PATCH 02/11] Fix gracefulControls drifting rotation up --- script-archive/gracefulControls.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/script-archive/gracefulControls.js b/script-archive/gracefulControls.js index 3179044c2c..562c80e574 100644 --- a/script-archive/gracefulControls.js +++ b/script-archive/gracefulControls.js @@ -111,8 +111,11 @@ function update(dt) { var x = Reticle.getPosition().x; var y = Reticle.getPosition().y; - yawFromMouse += ((x - lastX) * movementParameters.MOUSE_YAW_SCALE * movementParameters.MOUSE_SENSITIVITY); - pitchFromMouse += ((y - lastY) * movementParameters.MOUSE_PITCH_SCALE * movementParameters.MOUSE_SENSITIVITY); + var dx = x - lastX; + var dy = y - lastY; + + yawFromMouse += (dx * movementParameters.MOUSE_YAW_SCALE * movementParameters.MOUSE_SENSITIVITY); + pitchFromMouse += (dy * movementParameters.MOUSE_PITCH_SCALE * movementParameters.MOUSE_SENSITIVITY); pitchFromMouse = Math.max(-180, Math.min(180, pitchFromMouse)); resetCursorPosition(); @@ -170,8 +173,8 @@ function scriptEnding() { } function resetCursorPosition() { - var newX = Window.x + Window.innerWidth / 2; - var newY = Window.y + Window.innerHeight / 2; + var newX = Math.floor(Window.x + Window.innerWidth / 2); + var newY = Math.floor(Window.y + Window.innerHeight / 2); Reticle.setPosition({ x: newX, y: newY}); lastX = newX; lastY = newY; From e2f966b313e9aee074d6bf04687d871b62245acc Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Fri, 26 May 2017 13:16:45 -0700 Subject: [PATCH 03/11] Update gracefulControls to use setInterval instead of update --- script-archive/gracefulControls.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/script-archive/gracefulControls.js b/script-archive/gracefulControls.js index 562c80e574..fac1a0f137 100644 --- a/script-archive/gracefulControls.js +++ b/script-archive/gracefulControls.js @@ -35,6 +35,9 @@ var BRAKE_PARAMETERS = { MOUSE_SENSITIVITY: 0.5, } +var UPDATE_RATE = 90; +var USE_INTERVAL = true; + var movementParameters = DEFAULT_PARAMETERS; // Movement keys @@ -189,6 +192,8 @@ function toggleEnabled() { } } + +var timerID = null; function enable() { if (!enabled && Window.hasFocus()) { enabled = true; @@ -206,7 +211,17 @@ function enable() { Controller.captureKeyEvents({ text: CAPTURED_KEYS[i] }); } Reticle.setVisible(false); - Script.update.connect(update); + if (USE_INTERVAL) { + var lastTime = Date.now(); + timerID = Script.setInterval(function() { + var now = Date.now(); + var dt = now - lastTime; + lastTime = now; + update(dt / 1000); + }, (1.0 / UPDATE_RATE) * 1000); + } else { + Script.update.connect(update); + } } } @@ -217,7 +232,12 @@ function disable() { Controller.releaseKeyEvents({ text: CAPTURED_KEYS[i] }); } Reticle.setVisible(true); - Script.update.disconnect(update); + if (USE_INTERVAL) { + Script.clearInterval(timerID); + timerID = null; + } else { + Script.update.disconnect(update); + } } } From 0bd849d691e689c6b20b5a10b26df0d8e15df58c Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Fri, 26 May 2017 13:17:22 -0700 Subject: [PATCH 04/11] Fix avatar driving in gracefulControls --- script-archive/gracefulControls.js | 54 +++++++++++++++++++----------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/script-archive/gracefulControls.js b/script-archive/gracefulControls.js index fac1a0f137..371d83b146 100644 --- a/script-archive/gracefulControls.js +++ b/script-archive/gracefulControls.js @@ -35,6 +35,7 @@ var BRAKE_PARAMETERS = { MOUSE_SENSITIVITY: 0.5, } +var DRIVE_AVATAR_ENABLED = true; var UPDATE_RATE = 90; var USE_INTERVAL = true; @@ -50,7 +51,13 @@ var KEY_UP = "e"; var KEY_DOWN = "c"; var KEY_TOGGLE= "SPACE"; var KEY_DISABLE = "ESC"; -var CAPTURED_KEYS = [KEY_BRAKE, KEY_FORWARD, KEY_BACKWARD, KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN, KEY_TOGGLE]; +var CAPTURED_KEYS; + +if (DRIVE_AVATAR_ENABLED) { + CAPTURED_KEYS = [KEY_TOGGLE, KEY_BRAKE, KEY_FORWARD, KEY_BACKWARD, KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN]; +} else { + CAPTURED_KEYS = [KEY_TOGGLE]; +} // Global Variables var keys = {}; @@ -143,28 +150,35 @@ function update(dt) { MyAvatar.headPitch = newPitch; pitchFromMouse -= pitchMove; - // If force isn't being applied in a direction, add drag; - if (targetVelocity.x == 0) { - targetVelocity.x -= (velocity.x * movementParameters.DRAG_COEFFICIENT * dt); - } - if (targetVelocity.z == 0) { - targetVelocity.z -= (velocity.z * movementParameters.DRAG_COEFFICIENT * dt); - } - velocity = Vec3.sum(velocity, targetVelocity); - var maxSpeed = movementParameters.MAX_SPEED; - velocity.x = Math.max(-maxSpeed, Math.min(maxSpeed, velocity.x)); - velocity.z = Math.max(-maxSpeed, Math.min(maxSpeed, velocity.z)); - var v = Vec3.multiplyQbyV(MyAvatar.headOrientation, velocity); + if (DRIVE_AVATAR_ENABLED) { + // If force isn't being applied in a direction, add drag; + var drag = Math.min(movementParameters.DRAG_COEFFICIENT * dt, 1.0); + if (targetVelocity.x == 0) { + targetVelocity.x -= (velocity.x * drag); + } + if (targetVelocity.z == 0) { + targetVelocity.z -= (velocity.z * drag); + } + velocity = Vec3.sum(velocity, targetVelocity); - if (targetVelocityVertical == 0) { - targetVelocityVertical -= (velocityVertical * movementParameters.DRAG_COEFFICIENT * dt); + var maxSpeed = movementParameters.MAX_SPEED; + velocity.x = Math.max(-maxSpeed, Math.min(maxSpeed, velocity.x)); + velocity.z = Math.max(-maxSpeed, Math.min(maxSpeed, velocity.z)); + var v = Vec3.multiplyQbyV(MyAvatar.headOrientation, velocity); + + if (targetVelocityVertical == 0) { + targetVelocityVertical -= (velocityVertical * movementParameters.DRAG_COEFFICIENT * dt); + } + velocityVertical += targetVelocityVertical; + velocityVertical = Math.max(-maxSpeed, Math.min(maxSpeed, velocityVertical)); + v.y += velocityVertical; + + MyAvatar.motorReferenceFrame = 'world'; + MyAvatar.motorVelocity = v; + MyAvatar.motorTimescale = 1; + Vec3.print('vel', v); } - velocityVertical += targetVelocityVertical; - velocityVertical = Math.max(-maxSpeed, Math.min(maxSpeed, velocityVertical)); - v.y += velocityVertical; - - MyAvatar.setVelocity(v); } function vecToString(vec) { From ef1efa58d3f1683e91044f30f8c2909c68300bcf Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Fri, 26 May 2017 13:32:03 -0700 Subject: [PATCH 05/11] Make avatar driving behavior in gracefulControls more responsive --- script-archive/gracefulControls.js | 67 ++++++++++++++++-------------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/script-archive/gracefulControls.js b/script-archive/gracefulControls.js index 371d83b146..bf34608d10 100644 --- a/script-archive/gracefulControls.js +++ b/script-archive/gracefulControls.js @@ -12,9 +12,9 @@ var DEFAULT_PARAMETERS = { // Coefficient to use for linear drag. Higher numbers will cause motion to // slow down more quickly. - DRAG_COEFFICIENT: 0.9, - MAX_SPEED: 40.0, - ACCELERATION: 1.0, + DRAG_COEFFICIENT: 60.0, + MAX_SPEED: 10.0, + ACCELERATION: 10.0, MOUSE_YAW_SCALE: -0.125, MOUSE_PITCH_SCALE: -0.125, @@ -93,30 +93,6 @@ function keyReleaseEvent(event) { } function update(dt) { - var maxMove = 3.0 * dt; - var targetVelocity = { x: 0, y: 0, z: 0 }; - var targetVelocityVertical = 0; - var acceleration = movementParameters.ACCELERATION; - - if (keys[KEY_FORWARD]) { - targetVelocity.z -= acceleration * dt; - } - if (keys[KEY_LEFT]) { - targetVelocity.x -= acceleration * dt; - } - if (keys[KEY_BACKWARD]) { - targetVelocity.z += acceleration * dt; - } - if (keys[KEY_RIGHT]) { - targetVelocity.x += acceleration * dt; - } - if (keys[KEY_UP]) { - targetVelocityVertical += acceleration * dt; - } - if (keys[KEY_DOWN]) { - targetVelocityVertical -= acceleration * dt; - } - if (enabled && Window.hasFocus()) { var x = Reticle.getPosition().x; var y = Reticle.getPosition().y; @@ -152,13 +128,36 @@ function update(dt) { if (DRIVE_AVATAR_ENABLED) { + var targetVelocity = { x: 0, y: 0, z: 0 }; + var targetVelocityVertical = 0; + var acceleration = movementParameters.ACCELERATION; + + if (keys[KEY_FORWARD]) { + targetVelocity.z -= acceleration * dt; + } + if (keys[KEY_LEFT]) { + targetVelocity.x -= acceleration * dt; + } + if (keys[KEY_BACKWARD]) { + targetVelocity.z += acceleration * dt; + } + if (keys[KEY_RIGHT]) { + targetVelocity.x += acceleration * dt; + } + if (keys[KEY_UP]) { + targetVelocityVertical += acceleration * dt; + } + if (keys[KEY_DOWN]) { + targetVelocityVertical -= acceleration * dt; + } + // If force isn't being applied in a direction, add drag; - var drag = Math.min(movementParameters.DRAG_COEFFICIENT * dt, 1.0); + var drag = Math.max(movementParameters.DRAG_COEFFICIENT * dt, 1.0); if (targetVelocity.x == 0) { - targetVelocity.x -= (velocity.x * drag); + targetVelocity.x = -velocity.x * drag; } if (targetVelocity.z == 0) { - targetVelocity.z -= (velocity.z * drag); + targetVelocity.z = -velocity.z * drag; } velocity = Vec3.sum(velocity, targetVelocity); @@ -174,9 +173,7 @@ function update(dt) { velocityVertical = Math.max(-maxSpeed, Math.min(maxSpeed, velocityVertical)); v.y += velocityVertical; - MyAvatar.motorReferenceFrame = 'world'; MyAvatar.motorVelocity = v; - MyAvatar.motorTimescale = 1; Vec3.print('vel', v); } } @@ -220,6 +217,11 @@ function enable() { yawSpeed = 0; pitchSpeed = 0; velocityVertical = 0; + velocity = { x: 0, y: 0, z: 0 }; + + MyAvatar.motorReferenceFrame = 'world'; + MyAvatar.motorVelocity = { x: 0, y: 0, z: 0 }; + MyAvatar.motorTimescale = 1; for (var i = 0; i < CAPTURED_KEYS.length; i++) { Controller.captureKeyEvents({ text: CAPTURED_KEYS[i] }); @@ -246,6 +248,7 @@ function disable() { Controller.releaseKeyEvents({ text: CAPTURED_KEYS[i] }); } Reticle.setVisible(true); + MyAvatar.motorVelocity = { x: 0, y: 0, z: 0 }; if (USE_INTERVAL) { Script.clearInterval(timerID); timerID = null; From ae55e3969eacfb318bfdb88faff700d6753491bb Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Fri, 26 May 2017 16:33:54 -0700 Subject: [PATCH 06/11] Adjust gracefulControls to be slower --- script-archive/gracefulControls.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script-archive/gracefulControls.js b/script-archive/gracefulControls.js index bf34608d10..5dda41aba9 100644 --- a/script-archive/gracefulControls.js +++ b/script-archive/gracefulControls.js @@ -21,7 +21,7 @@ var DEFAULT_PARAMETERS = { MOUSE_SENSITIVITY: 0.5, // Damping frequency, adjust to change mouse look behavior - W: 4.2, + W: 2.2, } var BRAKE_PARAMETERS = { From fdedfeb49ae56ca79752674ed87a94049350db61 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Tue, 30 May 2017 13:47:36 -0700 Subject: [PATCH 07/11] Update gracefulControls to use controller mapping --- script-archive/gracefulControls.js | 89 +++++++++++++++++------------- 1 file changed, 51 insertions(+), 38 deletions(-) diff --git a/script-archive/gracefulControls.js b/script-archive/gracefulControls.js index 5dda41aba9..85e12d740c 100644 --- a/script-archive/gracefulControls.js +++ b/script-archive/gracefulControls.js @@ -41,18 +41,18 @@ var USE_INTERVAL = true; var movementParameters = DEFAULT_PARAMETERS; -// Movement keys -var KEY_BRAKE = "q"; -var KEY_FORWARD = "w"; -var KEY_BACKWARD = "s"; -var KEY_LEFT = "a"; -var KEY_RIGHT = "d"; -var KEY_UP = "e"; -var KEY_DOWN = "c"; -var KEY_TOGGLE= "SPACE"; -var KEY_DISABLE = "ESC"; -var CAPTURED_KEYS; +// Movement keys +var KEY_BRAKE = "Q"; +var KEY_FORWARD = "W"; +var KEY_BACKWARD = "S"; +var KEY_LEFT = "A"; +var KEY_RIGHT = "D"; +var KEY_UP = "E"; +var KEY_DOWN = "C"; +var KEY_TOGGLE= "Space"; + +var CAPTURED_KEYS; if (DRIVE_AVATAR_ENABLED) { CAPTURED_KEYS = [KEY_TOGGLE, KEY_BRAKE, KEY_FORWARD, KEY_BACKWARD, KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN]; } else { @@ -73,24 +73,6 @@ var pitchFromMouse = 0; var yawSpeed = 0; var pitchSpeed = 0; -function keyPressEvent(event) { - if (event.text == KEY_DISABLE) { - disable(); - } else if (event.text == KEY_TOGGLE) { - toggleEnabled(); - } else if (event.text == KEY_BRAKE) { - movementParameters = BRAKE_PARAMETERS; - } - keys[event.text] = true; -} - -function keyReleaseEvent(event) { - if (event.text == KEY_BRAKE) { - movementParameters = DEFAULT_PARAMETERS; - } - - delete keys[event.text]; -} function update(dt) { if (enabled && Window.hasFocus()) { @@ -174,7 +156,6 @@ function update(dt) { v.y += velocityVertical; MyAvatar.motorVelocity = v; - Vec3.print('vel', v); } } @@ -184,6 +165,7 @@ function vecToString(vec) { function scriptEnding() { disable(); + Controller.disableMapping(MAPPING_NAME); } function resetCursorPosition() { @@ -223,9 +205,6 @@ function enable() { MyAvatar.motorVelocity = { x: 0, y: 0, z: 0 }; MyAvatar.motorTimescale = 1; - for (var i = 0; i < CAPTURED_KEYS.length; i++) { - Controller.captureKeyEvents({ text: CAPTURED_KEYS[i] }); - } Reticle.setVisible(false); if (USE_INTERVAL) { var lastTime = Date.now(); @@ -244,11 +223,11 @@ function enable() { function disable() { if (enabled) { enabled = false; - for (var i = 0; i < CAPTURED_KEYS.length; i++) { - Controller.releaseKeyEvents({ text: CAPTURED_KEYS[i] }); - } + Controller.disableMapping(MAPPING_NAME); Reticle.setVisible(true); + MyAvatar.motorVelocity = { x: 0, y: 0, z: 0 }; + if (USE_INTERVAL) { Script.clearInterval(timerID); timerID = null; @@ -258,7 +237,41 @@ function disable() { } } -Controller.keyPressEvent.connect(keyPressEvent); -Controller.keyReleaseEvent.connect(keyReleaseEvent); + +var MAPPING_NAME = 'io.highfidelity.gracefulControls'; +var controllerMapping = Controller.newMapping(MAPPING_NAME); + +function onKeyPress(key, value) { + print(key, value); + keys[key] = value > 0; + + if (value > 0) { + if (key == KEY_TOGGLE) { + toggleEnabled(); + } else if (key == KEY_BRAKE) { + movementParameters = BRAKE_PARAMETERS; + } + } else { + if (key == KEY_BRAKE) { + movementParameters = DEFAULT_PARAMETERS; + } + } +} + +for (var i = 0; i < CAPTURED_KEYS.length; ++i) { + var key = CAPTURED_KEYS[i]; + var hw = Controller.Hardware.Keyboard[key]; + if (hw) { + controllerMapping.from(hw).to(function(key) { + return function(value) { + onKeyPress(key, value); + }; + }(key)); + } else { + print("Unknown key: ", key); + } +} + +Controller.enableMapping(MAPPING_NAME); Script.scriptEnding.connect(scriptEnding); From e562ea197b085a8ea70057f923b99ce0dfcd4e0c Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Tue, 30 May 2017 13:49:17 -0700 Subject: [PATCH 08/11] Rename CAPTURED_KEYS to KEYS in gracefulControls.js --- script-archive/gracefulControls.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/script-archive/gracefulControls.js b/script-archive/gracefulControls.js index 85e12d740c..982eb6cefa 100644 --- a/script-archive/gracefulControls.js +++ b/script-archive/gracefulControls.js @@ -52,11 +52,11 @@ var KEY_UP = "E"; var KEY_DOWN = "C"; var KEY_TOGGLE= "Space"; -var CAPTURED_KEYS; +var KEYS; if (DRIVE_AVATAR_ENABLED) { - CAPTURED_KEYS = [KEY_TOGGLE, KEY_BRAKE, KEY_FORWARD, KEY_BACKWARD, KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN]; + KEYS = [KEY_TOGGLE, KEY_BRAKE, KEY_FORWARD, KEY_BACKWARD, KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN]; } else { - CAPTURED_KEYS = [KEY_TOGGLE]; + KEYS = [KEY_TOGGLE]; } // Global Variables @@ -258,8 +258,8 @@ function onKeyPress(key, value) { } } -for (var i = 0; i < CAPTURED_KEYS.length; ++i) { - var key = CAPTURED_KEYS[i]; +for (var i = 0; i < KEYS.length; ++i) { + var key = KEYS[i]; var hw = Controller.Hardware.Keyboard[key]; if (hw) { controllerMapping.from(hw).to(function(key) { From 76bb518537393cb9a4ba388882c7aab75a016d66 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Tue, 30 May 2017 14:03:26 -0700 Subject: [PATCH 09/11] Fix gracefulControls not able to be re-enabled --- script-archive/gracefulControls.js | 1 - 1 file changed, 1 deletion(-) diff --git a/script-archive/gracefulControls.js b/script-archive/gracefulControls.js index 982eb6cefa..42ce5df547 100644 --- a/script-archive/gracefulControls.js +++ b/script-archive/gracefulControls.js @@ -223,7 +223,6 @@ function enable() { function disable() { if (enabled) { enabled = false; - Controller.disableMapping(MAPPING_NAME); Reticle.setVisible(true); MyAvatar.motorVelocity = { x: 0, y: 0, z: 0 }; From b95a2180e832df62bc7c77e616c32106f2d7c3fb Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Tue, 30 May 2017 14:14:29 -0700 Subject: [PATCH 10/11] Fix stuck movement in gracefulControls.js --- script-archive/gracefulControls.js | 33 ++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/script-archive/gracefulControls.js b/script-archive/gracefulControls.js index 42ce5df547..91c53d8c4b 100644 --- a/script-archive/gracefulControls.js +++ b/script-archive/gracefulControls.js @@ -54,9 +54,9 @@ var KEY_TOGGLE= "Space"; var KEYS; if (DRIVE_AVATAR_ENABLED) { - KEYS = [KEY_TOGGLE, KEY_BRAKE, KEY_FORWARD, KEY_BACKWARD, KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN]; + KEYS = [KEY_BRAKE, KEY_FORWARD, KEY_BACKWARD, KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN]; } else { - KEYS = [KEY_TOGGLE]; + KEYS = []; } // Global Variables @@ -163,11 +163,6 @@ function vecToString(vec) { return vec.x + ", " + vec.y + ", " + vec.z; } -function scriptEnding() { - disable(); - Controller.disableMapping(MAPPING_NAME); -} - function resetCursorPosition() { var newX = Math.floor(Window.x + Window.innerWidth / 2); var newY = Math.floor(Window.y + Window.innerHeight / 2); @@ -205,6 +200,8 @@ function enable() { MyAvatar.motorVelocity = { x: 0, y: 0, z: 0 }; MyAvatar.motorTimescale = 1; + Controller.enableMapping(MAPPING_KEYS_NAME); + Reticle.setVisible(false); if (USE_INTERVAL) { var lastTime = Date.now(); @@ -227,6 +224,8 @@ function disable() { MyAvatar.motorVelocity = { x: 0, y: 0, z: 0 }; + Controller.disableMapping(MAPPING_KEYS_NAME); + if (USE_INTERVAL) { Script.clearInterval(timerID); timerID = null; @@ -236,9 +235,17 @@ function disable() { } } +function scriptEnding() { + disable(); + Controller.disableMapping(MAPPING_ENABLE_NAME); + Controller.disableMapping(MAPPING_KEYS_NAME); +} -var MAPPING_NAME = 'io.highfidelity.gracefulControls'; -var controllerMapping = Controller.newMapping(MAPPING_NAME); + +var MAPPING_ENABLE_NAME = 'io.highfidelity.gracefulControls.toggle'; +var MAPPING_KEYS_NAME = 'io.highfidelity.gracefulControls.keys'; +var keyControllerMapping = Controller.newMapping(MAPPING_KEYS_NAME); +var enableControllerMapping = Controller.newMapping(MAPPING_ENABLE_NAME); function onKeyPress(key, value) { print(key, value); @@ -261,7 +268,7 @@ for (var i = 0; i < KEYS.length; ++i) { var key = KEYS[i]; var hw = Controller.Hardware.Keyboard[key]; if (hw) { - controllerMapping.from(hw).to(function(key) { + keyControllerMapping.from(hw).to(function(key) { return function(value) { onKeyPress(key, value); }; @@ -271,6 +278,10 @@ for (var i = 0; i < KEYS.length; ++i) { } } -Controller.enableMapping(MAPPING_NAME); +enableControllerMapping.from(Controller.Hardware.Keyboard[KEY_TOGGLE]).to(function(value) { + onKeyPress(KEY_TOGGLE, value); +}); + +Controller.enableMapping(MAPPING_ENABLE_NAME); Script.scriptEnding.connect(scriptEnding); From 2680fb15948e68eeea452ec2d4d63bbe3da89cec Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Thu, 1 Jun 2017 13:23:21 -0700 Subject: [PATCH 11/11] Fix gracefulControls not working on multi-monitor setups --- script-archive/gracefulControls.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/script-archive/gracefulControls.js b/script-archive/gracefulControls.js index 91c53d8c4b..91e31cd243 100644 --- a/script-archive/gracefulControls.js +++ b/script-archive/gracefulControls.js @@ -65,8 +65,9 @@ var velocity = { x: 0, y: 0, z: 0 }; var velocityVertical = 0; var enabled = false; -var lastX = Reticle.getPosition().x; -var lastY = Reticle.getPosition().y; +var pos = Reticle.getPosition(); +var lastX = pos.x; +var lastY = pos.y; var yawFromMouse = 0; var pitchFromMouse = 0; @@ -76,8 +77,9 @@ var pitchSpeed = 0; function update(dt) { if (enabled && Window.hasFocus()) { - var x = Reticle.getPosition().x; - var y = Reticle.getPosition().y; + var pos = Reticle.getPosition(); + var x = pos.x; + var y = pos.y; var dx = x - lastX; var dy = y - lastY; @@ -164,9 +166,9 @@ function vecToString(vec) { } function resetCursorPosition() { - var newX = Math.floor(Window.x + Window.innerWidth / 2); - var newY = Math.floor(Window.y + Window.innerHeight / 2); - Reticle.setPosition({ x: newX, y: newY}); + var newX = Math.floor(Window.innerWidth / 2); + var newY = Math.floor(Window.innerHeight / 2); + Reticle.setPosition({ x: newX, y: newY }); lastX = newX; lastY = newY; }