388 lines
12 KiB
JavaScript
Raw Normal View History

2015-11-02 20:40:40 -08:00
"use strict";
2016-08-23 12:34:32 -07:00
2015-11-03 15:36:02 -08:00
//
// away.js
2016-08-23 12:34:32 -07:00
//
2015-11-03 15:36:02 -08:00
// examples
//
// Created by Howard Stearns 11/3/15
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
2015-11-02 20:40:40 -08:00
// 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.
2016-08-23 12:34:32 -07:00
/* eslint indent: ["error", 4, { "outerIIFEBody": 0 }] */
2016-08-23 12:34:32 -07:00
(function() { // BEGIN LOCAL_SCOPE
2016-10-08 13:45:18 -07:00
var BASIC_TIMER_INTERVAL = 50; // 50ms = 20hz
var OVERLAY_WIDTH = 1920;
var OVERLAY_HEIGHT = 1080;
2015-11-02 20:40:40 -08:00
var OVERLAY_DATA = {
2016-03-01 10:43:49 -08:00
width: OVERLAY_WIDTH,
height: OVERLAY_HEIGHT,
2018-03-16 11:09:53 -07:00
imageURL: Script.resolvePath("assets/images/Overlay-Viz-blank.png"),
emissive: true,
drawInFront: true,
alpha: 1
2015-11-02 20:40:40 -08:00
};
var AVATAR_MOVE_FOR_ACTIVE_DISTANCE = 0.8; // meters -- no longer away if avatar moves this far while away
2015-11-02 20:40:40 -08:00
var CAMERA_MATRIX = -7;
2016-02-29 21:25:25 -08:00
var OVERLAY_DATA_HMD = {
localPosition: {x: 0, y: 0, z: -1 * MyAvatar.sensorToWorldScale},
localRotation: {x: 0, y: 0, z: 0, w: 1},
2016-02-29 21:25:25 -08:00
width: OVERLAY_WIDTH,
height: OVERLAY_HEIGHT,
2018-03-16 11:09:53 -07:00
url: Script.resolvePath("assets/images/Overlay-Viz-blank.png"),
2016-02-29 21:25:25 -08:00
color: {red: 255, green: 255, blue: 255},
alpha: 1,
scale: 2 * MyAvatar.sensorToWorldScale,
emissive: true,
drawInFront: true,
parentID: MyAvatar.SELF_ID,
parentJointIndex: CAMERA_MATRIX,
ignorePickIntersection: true
2016-02-29 21:25:25 -08:00
};
var AWAY_INTRO = {
url: "http://hifi-content.s3.amazonaws.com/ozan/dev/anim/standard_anims_160127/kneel.fbx",
playbackRate: 30.0,
loopFlag: false,
startFrame: 0.0,
endFrame: 83.0
2016-02-29 21:25:25 -08:00
};
2016-10-08 13:45:18 -07:00
// MAIN CONTROL
var isEnabled = true;
var wasMuted; // unknonwn?
var isAway = false; // we start in the un-away state
var eventMappingName = "io.highfidelity.away"; // goActive on hand controller button events, too.
var eventMapping = Controller.newMapping(eventMappingName);
var avatarPosition = MyAvatar.position;
var wasHmdMounted = HMD.mounted;
var previousBubbleState = Users.getIgnoreRadiusEnabled();
2016-10-08 13:45:18 -07:00
var enterAwayStateWhenFocusLostInVR = HMD.enterAwayStateWhenFocusLostInVR;
2016-10-08 13:45:18 -07:00
// some intervals we may create/delete
var avatarMovedInterval;
2016-04-20 01:27:10 -07:00
// prefetch the kneel animation and hold a ref so it's always resident in memory when we need it.
var _animation = AnimationCache.prefetch(AWAY_INTRO.url);
2015-11-02 20:40:40 -08:00
function playAwayAnimation() {
2016-08-23 12:34:32 -07:00
MyAvatar.overrideAnimation(AWAY_INTRO.url,
AWAY_INTRO.playbackRate,
AWAY_INTRO.loopFlag,
AWAY_INTRO.startFrame,
AWAY_INTRO.endFrame);
2015-11-02 20:40:40 -08:00
}
2015-11-02 20:40:40 -08:00
function stopAwayAnimation() {
MyAvatar.restoreAnimation();
2015-11-02 20:40:40 -08:00
}
// OVERLAY
var overlay = Overlays.addOverlay("image", OVERLAY_DATA);
2016-02-29 21:25:25 -08:00
var overlayHMD = Overlays.addOverlay("image3d", OVERLAY_DATA_HMD);
2016-03-01 10:43:49 -08:00
2015-11-02 20:40:40 -08:00
function showOverlay() {
if (HMD.active) {
2016-03-01 10:43:49 -08:00
// make sure desktop version is hidden
Overlays.editOverlay(overlay, { visible: false });
Overlays.editOverlay(overlayHMD, { visible: true });
} else {
2016-03-01 10:43:49 -08:00
// make sure HMD is hidden
Overlays.editOverlay(overlayHMD, { visible: false });
2016-02-29 21:25:25 -08:00
// Update for current screen size, keeping overlay proportions constant.
2016-03-01 10:43:49 -08:00
var screen = Controller.getViewportDimensions();
2016-02-29 21:25:25 -08:00
2016-03-01 10:43:49 -08:00
// keep the overlay it's natural size and always center it...
Overlays.editOverlay(overlay, {
visible: true,
x: ((screen.x - OVERLAY_WIDTH) / 2),
y: ((screen.y - OVERLAY_HEIGHT) / 2)
});
}
2015-11-02 20:40:40 -08:00
}
2015-11-02 20:40:40 -08:00
function hideOverlay() {
Overlays.editOverlay(overlay, {visible: false});
2016-02-29 21:25:25 -08:00
Overlays.editOverlay(overlayHMD, {visible: false});
2015-11-02 20:40:40 -08:00
}
2015-11-02 20:40:40 -08:00
hideOverlay();
function maybeMoveOverlay() {
2016-03-01 10:43:49 -08:00
if (isAway) {
// if we switched from HMD to Desktop, make sure to hide our HUD overlay and show the
// desktop overlay
if (!HMD.active) {
showOverlay(); // this will also recenter appropriately
}
if (HMD.active) {
var sensorScaleFactor = MyAvatar.sensorToWorldScale;
var localPosition = {x: 0, y: 0, z: -1 * sensorScaleFactor};
Overlays.editOverlay(overlayHMD, { visible: true, localPosition: localPosition, scale: 2 * sensorScaleFactor });
2016-03-01 10:43:49 -08:00
// make sure desktop version is hidden
Overlays.editOverlay(overlay, { visible: false });
2016-10-08 13:45:18 -07:00
// also remember avatar position
avatarPosition = MyAvatar.position;
2016-03-01 10:43:49 -08:00
}
}
}
2015-11-02 20:40:40 -08:00
function ifAvatarMovedGoActive() {
2016-10-08 13:45:18 -07:00
var newAvatarPosition = MyAvatar.position;
if (Vec3.distance(newAvatarPosition, avatarPosition) > AVATAR_MOVE_FOR_ACTIVE_DISTANCE) {
goActive();
}
2016-10-08 13:45:18 -07:00
avatarPosition = newAvatarPosition;
}
2016-10-08 13:45:18 -07:00
function goAway(fromStartup) {
if (!isEnabled || isAway) {
2015-11-02 20:40:40 -08:00
return;
}
// If we're entering away mode from some other state than startup, then we create our move timer immediately.
// However if we're just stating up, we need to delay this process so that we don't think the initial teleport
// is actually a move.
if (fromStartup === undefined || fromStartup === false) {
avatarMovedInterval = Script.setInterval(ifAvatarMovedGoActive, BASIC_TIMER_INTERVAL);
} else {
var WAIT_FOR_MOVE_ON_STARTUP = 3000; // 3 seconds
Script.setTimeout(function() {
avatarMovedInterval = Script.setInterval(ifAvatarMovedGoActive, BASIC_TIMER_INTERVAL);
}, WAIT_FOR_MOVE_ON_STARTUP);
}
previousBubbleState = Users.getIgnoreRadiusEnabled();
if (!previousBubbleState) {
Users.toggleIgnoreRadius();
}
UserActivityLogger.privacyShieldToggled(Users.getIgnoreRadiusEnabled());
2016-06-22 09:06:59 -07:00
UserActivityLogger.toggledAway(true);
MyAvatar.isAway = true;
}
function goActive() {
if (!isAway) {
return;
}
UserActivityLogger.toggledAway(false);
MyAvatar.isAway = false;
if (Users.getIgnoreRadiusEnabled() !== previousBubbleState) {
Users.toggleIgnoreRadius();
UserActivityLogger.privacyShieldToggled(Users.getIgnoreRadiusEnabled());
}
if (!Window.hasFocus()) {
Window.setFocus();
}
}
2016-06-22 09:06:59 -07:00
MyAvatar.wentAway.connect(setAwayProperties);
MyAvatar.wentActive.connect(setActiveProperties);
function setAwayProperties() {
2015-11-02 20:40:40 -08:00
isAway = true;
2017-06-13 16:02:58 -04:00
wasMuted = Audio.muted;
2015-11-02 20:40:40 -08:00
if (!wasMuted) {
2017-06-13 16:02:58 -04:00
Audio.muted = !Audio.muted;
2015-11-02 20:40:40 -08:00
}
MyAvatar.setEnableMeshVisible(false); // just for our own display, without changing point of view
2015-11-02 20:40:40 -08:00
playAwayAnimation(); // animation is still seen by others
showOverlay();
2016-02-29 20:23:06 -08:00
HMD.requestShowHandControllers();
2016-02-29 20:23:06 -08:00
// tell the Reticle, we want to stop capturing the mouse until we come back
Reticle.allowMouseCapture = false;
2016-06-07 09:50:10 -07:00
// Allow users to find their way to other applications, our menus, etc.
// For desktop, that means we want the reticle visible.
// For HMD, the hmd preview will show the system mouse because of allowMouseCapture,
// but we want to turn off our Reticle so that we don't get two in preview and a stuck one in headset.
Reticle.visible = !HMD.active;
2016-09-23 16:17:28 -07:00
wasHmdMounted = HMD.mounted; // always remember the correct state
avatarPosition = MyAvatar.position;
2015-11-02 20:40:40 -08:00
}
function setActiveProperties() {
isAway = false;
2019-04-30 14:34:10 -07:00
if (Audio.muted && !wasMuted) {
Audio.muted = false;
}
MyAvatar.setEnableMeshVisible(true); // IWBNI we respected Developer->Avatar->Draw Mesh setting.
stopAwayAnimation();
HMD.requestHideHandControllers();
// update the UI sphere to be centered about the current HMD orientation.
HMD.centerUI();
// forget about any IK joint limits
MyAvatar.clearIKJointLimitHistory();
// update the avatar hips to point in the same direction as the HMD orientation.
MyAvatar.centerBody();
hideOverlay();
2016-02-29 20:23:06 -08:00
// tell the Reticle, we are ready to capture the mouse again and it should be visible
Reticle.allowMouseCapture = true;
Reticle.visible = true;
if (HMD.active) {
Reticle.position = HMD.getHUDLookAtPosition2D();
}
2016-09-23 16:17:28 -07:00
wasHmdMounted = HMD.mounted; // always remember the correct state
2016-10-08 13:45:18 -07:00
Script.clearInterval(avatarMovedInterval);
}
2016-02-10 17:01:11 -08:00
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();
}
2016-02-10 17:01:11 -08:00
}
2016-03-01 15:00:19 -08:00
var wasHmdActive = HMD.active;
var wasMouseCaptured = Reticle.mouseCaptured;
2016-02-10 17:01:11 -08:00
function maybeGoAway() {
2016-09-23 16:17:28 -07:00
// If our active state change (went to or from HMD mode), and we are now in the HMD, go into away
if (HMD.active !== wasHmdActive) {
wasHmdActive = !wasHmdActive;
if (wasHmdActive) {
goAway();
2016-10-08 13:45:18 -07:00
return;
}
2015-11-02 20:40:40 -08:00
}
2016-08-23 12:34:32 -07:00
// If the mouse has gone from captured, to non-captured state, then it likely means the person is still in the HMD,
// but tabbed away from the application (meaning they don't have mouse control) and they likely want to go into
// an away state
if (Reticle.mouseCaptured !== wasMouseCaptured) {
wasMouseCaptured = !wasMouseCaptured;
if (!wasMouseCaptured) {
2019-05-09 18:08:36 +01:00
if (enterAwayStateWhenFocusLostInVR) {
goAway();
return;
}
}
}
// If you've removed your HMD from your head, and we can detect it, we will also go away...
if (HMD.mounted !== wasHmdMounted) {
2016-10-08 13:45:18 -07:00
wasHmdMounted = HMD.mounted;
2016-09-23 16:17:28 -07:00
print("HMD mounted changed...");
// We're putting the HMD on... switch to those devices
if (HMD.mounted) {
print("NOW mounted...");
} else {
print("HMD NOW un-mounted...");
if (HMD.active) {
goAway();
2016-10-08 13:45:18 -07:00
return;
2016-09-23 16:17:28 -07:00
}
}
}
2016-02-10 17:01:11 -08:00
}
function setEnabled(value) {
if (!value) {
goActive();
}
isEnabled = value;
}
function checkAudioToggled() {
if (isAway && !Audio.muted) {
goActive();
}
}
var CHANNEL_AWAY_ENABLE = "Hifi-Away-Enable";
var handleMessage = function(channel, message, sender) {
if (channel === CHANNEL_AWAY_ENABLE && sender === MyAvatar.sessionUUID) {
2016-10-25 15:28:11 -07:00
print("away.js | Got message on Hifi-Away-Enable: ", message);
setEnabled(message === 'enable');
}
};
Messages.subscribe(CHANNEL_AWAY_ENABLE);
Messages.messageReceived.connect(handleMessage);
var maybeIntervalTimer = Script.setInterval(function() {
2016-10-08 13:45:18 -07:00
maybeMoveOverlay();
maybeGoAway();
checkAudioToggled();
2016-10-08 13:45:18 -07:00
}, BASIC_TIMER_INTERVAL);
2016-02-11 13:05:47 -08:00
Controller.mousePressEvent.connect(goActive);
Controller.keyPressEvent.connect(maybeGoActive);
2016-02-11 13:27:56 -08:00
// Note peek() so as to not interfere with other mappings.
eventMapping.from(Controller.Standard.LeftPrimaryThumb).peek().to(goActive);
2016-02-11 13:27:56 -08:00
eventMapping.from(Controller.Standard.RightPrimaryThumb).peek().to(goActive);
eventMapping.from(Controller.Standard.LeftSecondaryThumb).peek().to(goActive);
eventMapping.from(Controller.Standard.RightSecondaryThumb).peek().to(goActive);
2016-05-12 15:41:15 -07:00
eventMapping.from(Controller.Standard.LT).peek().to(goActive);
eventMapping.from(Controller.Standard.LB).peek().to(goActive);
eventMapping.from(Controller.Standard.LS).peek().to(goActive);
eventMapping.from(Controller.Standard.LeftGrip).peek().to(goActive);
2016-05-12 15:41:15 -07:00
eventMapping.from(Controller.Standard.RT).peek().to(goActive);
eventMapping.from(Controller.Standard.RB).peek().to(goActive);
eventMapping.from(Controller.Standard.RS).peek().to(goActive);
eventMapping.from(Controller.Standard.RightGrip).peek().to(goActive);
2016-05-12 15:41:15 -07:00
eventMapping.from(Controller.Standard.Back).peek().to(goActive);
eventMapping.from(Controller.Standard.Start).peek().to(goActive);
2016-02-11 13:27:56 -08:00
Controller.enableMapping(eventMappingName);
2016-02-10 17:01:11 -08:00
function awayStateWhenFocusLostInVRChanged(enabled) {
2019-05-09 18:08:36 +01:00
enterAwayStateWhenFocusLostInVR = enabled;
}
2016-02-10 17:01:11 -08:00
Script.scriptEnding.connect(function () {
2016-10-08 13:45:18 -07:00
Script.clearInterval(maybeIntervalTimer);
2016-02-10 17:01:11 -08:00
goActive();
2019-05-09 18:08:36 +01:00
HMD.awayStateWhenFocusLostInVRChanged.disconnect(awayStateWhenFocusLostInVRChanged);
2016-02-11 13:27:56 -08:00
Controller.disableMapping(eventMappingName);
Controller.mousePressEvent.disconnect(goActive);
Controller.keyPressEvent.disconnect(maybeGoActive);
2016-10-25 15:28:11 -07:00
Messages.messageReceived.disconnect(handleMessage);
Messages.unsubscribe(CHANNEL_AWAY_ENABLE);
2016-05-12 15:41:15 -07:00
});
2016-08-23 12:34:32 -07:00
HMD.awayStateWhenFocusLostInVRChanged.connect(awayStateWhenFocusLostInVRChanged);
if (HMD.active && !HMD.mounted) {
print("Starting script, while HMD is active and not mounted...");
2016-10-08 13:45:18 -07:00
goAway(true);
}
2016-08-23 12:34:32 -07:00
}()); // END LOCAL_SCOPE