From 1b7ed3d847fa71d6df316bf36755b43daedf4c20 Mon Sep 17 00:00:00 2001 From: Howard Stearns Date: Mon, 2 Nov 2015 20:40:40 -0800 Subject: [PATCH] away script --- examples/away.js | 84 ++++++++++++++++++++++++++++++++++++++ examples/defaultScripts.js | 1 + 2 files changed, 85 insertions(+) create mode 100644 examples/away.js diff --git a/examples/away.js b/examples/away.js new file mode 100644 index 0000000000..9fd688dbb7 --- /dev/null +++ b/examples/away.js @@ -0,0 +1,84 @@ +"use strict"; +/*jslint vars: true, plusplus: true*/ +/*global HMD, AudioDevice, MyAvatar, Controller, Script, Overlays, print*/ + +// Goes into "paused" when the '.' key (and automatically when started in HMD), and normal when pressing any key. +// See MAIN CONTROL, below, for what "paused" actually does. + +var OVERLAY_DATA = { + text: "Paused:\npress any key to continue", + font: {size: 75}, + color: {red: 200, green: 255, blue: 255}, + alpha: 0.9 +}; + +// ANIMATION +// We currently don't have play/stopAnimation integrated with the animation graph, but we can get the same effect +// using an animation graph with a state that we turn on and off through the animation var defined with that state. +var awayAnimationHandlerId, activeAnimationHandlerId; +function playAwayAnimation() { + function animateAway() { + return {isAway: true, isNotAway: false, isNotMoving: false}; + } + awayAnimationHandlerId = MyAvatar.addAnimationStateHandler(animateAway, null); +} +function stopAwayAnimation() { + MyAvatar.removeAnimationStateHandler(awayAnimationHandlerId); + function animateActive(state) { + if (!state.isAway) { // Once the right state gets reflected back to us, we don't need the hander any more. + // But we are locked against handler changes during the execution of a handler, so remove asynchronously. + Script.setTimeout(function () { MyAvatar.removeAnimationStateHandler(activeAnimationHandlerId); }, 0); + } + return {isAway: false, isNotAway: true}; // IWBNI we had a way of deleting an anim var. + } + activeAnimationHandlerId = MyAvatar.addAnimationStateHandler(animateActive, ['isAway']); +} + +// OVERLAY +var overlay = Overlays.addOverlay("text", OVERLAY_DATA); +function showOverlay() { + var screen = Controller.getViewportDimensions(); + Overlays.editOverlay(overlay, {visible: true, x: screen.x / 4, y: screen.y / 4}); +} +function hideOverlay() { + Overlays.editOverlay(overlay, {visible: false}); +} +hideOverlay(); + + +// MAIN CONTROL +var wasMuted, isAway; +function goAway() { + if (isAway) { + return; + } + isAway = true; + print('going "away"'); + wasMuted = AudioDevice.getMuted(); + if (!wasMuted) { + AudioDevice.toggleMute(); + } + MyAvatar.setEnableMeshVisible(false); // just for our own display, without changing point of view + playAwayAnimation(); // animation is still seen by others + showOverlay(); +} +function goActive(event) { + if (!isAway) { + if (event.text === '.') { + goAway(); + } + return; + } + isAway = false; + print('going "active"'); + if (!wasMuted) { + AudioDevice.toggleMute(); + } + MyAvatar.setEnableMeshVisible(true); // IWBNI we respected Developer->Avatar->Draw Mesh setting. + stopAwayAnimation(); + hideOverlay(); +} +Controller.keyPressEvent.connect(goActive); +if (HMD.active) { + goAway(); +} diff --git a/examples/defaultScripts.js b/examples/defaultScripts.js index 53e7e43aa9..6efe6edef3 100644 --- a/examples/defaultScripts.js +++ b/examples/defaultScripts.js @@ -8,6 +8,7 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +Script.load("away.js"); Script.load("progress.js"); Script.load("edit.js"); Script.load("selectAudioDevice.js");