From 3e8387a8669eea0257db5bd63115c0443d2b272d Mon Sep 17 00:00:00 2001 From: Kalila L Date: Wed, 3 Feb 2021 23:13:58 -0500 Subject: [PATCH] Alter away functionality to add onEscape functionality. --- scripts/defaultScripts.js | 1 + scripts/system/away.js | 29 +++++++++++++---------------- scripts/system/onEscape.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 16 deletions(-) create mode 100644 scripts/system/onEscape.js diff --git a/scripts/defaultScripts.js b/scripts/defaultScripts.js index b27008e255..82b228008f 100644 --- a/scripts/defaultScripts.js +++ b/scripts/defaultScripts.js @@ -36,6 +36,7 @@ var DEFAULT_SCRIPTS_COMBINED = [ "system/inspect.js", "system/keyboardShortcuts/keyboardShortcuts.js", "system/checkForUpdates.js", + "system/onEscape.js", "system/onFirstRun.js", "system/appreciate/appreciate_app.js" ]; diff --git a/scripts/system/away.js b/scripts/system/away.js index ef79791e44..1cb247add4 100644 --- a/scripts/system/away.js +++ b/scripts/system/away.js @@ -3,8 +3,6 @@ // // away.js // -// examples -// // Created by Howard Stearns 11/3/15 // Copyright 2015 High Fidelity, Inc. // @@ -255,17 +253,6 @@ function setActiveProperties() { Script.clearInterval(avatarMovedInterval); } -function maybeGoActive(event) { - if (event.isAutoRepeat) { // isAutoRepeat is true when held down (or when Windows feels like it) - return; - } - if (!isAway && (event.text === 'ESC')) { - goAway(); - } else { - goActive(); - } -} - var wasHmdActive = HMD.active; var wasMouseCaptured = Reticle.mouseCaptured; @@ -329,12 +316,24 @@ var CHANNEL_AWAY_ENABLE = "Hifi-Away-Enable"; var handleMessage = function(channel, message, sender) { if (channel === CHANNEL_AWAY_ENABLE && sender === MyAvatar.sessionUUID) { print("away.js | Got message on Hifi-Away-Enable: ", message); - setEnabled(message === 'enable'); + if (message === 'enable') { + setEnabled(message === 'enable'); + } else if (message === 'toggle') { + toggleAway(); + } } }; Messages.subscribe(CHANNEL_AWAY_ENABLE); Messages.messageReceived.connect(handleMessage); +function toggleAway() { + if (!isAway) { + goAway(); + } else { + goActive(); + } +} + var maybeIntervalTimer = Script.setInterval(function() { maybeMoveOverlay(); maybeGoAway(); @@ -343,7 +342,6 @@ var maybeIntervalTimer = Script.setInterval(function() { Controller.mousePressEvent.connect(goActive); -Controller.keyPressEvent.connect(maybeGoActive); // Note peek() so as to not interfere with other mappings. eventMapping.from(Controller.Standard.LeftPrimaryThumb).peek().to(goActive); eventMapping.from(Controller.Standard.RightPrimaryThumb).peek().to(goActive); @@ -371,7 +369,6 @@ Script.scriptEnding.connect(function () { HMD.awayStateWhenFocusLostInVRChanged.disconnect(awayStateWhenFocusLostInVRChanged); Controller.disableMapping(eventMappingName); Controller.mousePressEvent.disconnect(goActive); - Controller.keyPressEvent.disconnect(maybeGoActive); Messages.messageReceived.disconnect(handleMessage); Messages.unsubscribe(CHANNEL_AWAY_ENABLE); }); diff --git a/scripts/system/onEscape.js b/scripts/system/onEscape.js new file mode 100644 index 0000000000..5160d4f42e --- /dev/null +++ b/scripts/system/onEscape.js @@ -0,0 +1,34 @@ +'use strict'; + +// +// onEscape.js +// +// Created by Kalila L. on Feb 3 2021. +// Copyright 2020 Vircadia contributors. +// +// This script manages actions when the user triggers an "escape" key or action. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +(function() { // BEGIN LOCAL_SCOPE + + function maybeEscapeKeyPressed (event) { + if (event.isAutoRepeat) { // isAutoRepeat is true when held down (or when Windows feels like it) + return; + } + + if (event.text === 'ESC') { + var CHANNEL_AWAY_ENABLE = 'Hifi-Away-Enable'; + Messages.sendMessage(CHANNEL_AWAY_ENABLE, 'toggle', true); + } + } + + Controller.keyPressEvent.connect(maybeEscapeKeyPressed); + + Script.scriptEnding.connect(function () { + Controller.keyPressEvent.disconnect(maybeEscapeKeyPressed); + }); + +}());