290 lines
14 KiB
JavaScript
Raw Normal View History

2017-08-31 18:06:55 -07:00
"use strict";
2017-08-23 17:32:41 -07:00
2017-11-13 13:37:53 -08:00
// webSurfaceLaserInput.js
2017-08-23 17:32:41 -07:00
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
/* global Script, Entities, enableDispatcherModule, disableDispatcherModule, makeRunningValues,
makeDispatcherModuleParameters, Overlays, HMD, TRIGGER_ON_VALUE, TRIGGER_OFF_VALUE, getEnabledModuleByName,
ContextOverlay, Picks, makeLaserParams, Settings, MyAvatar, RIGHT_HAND, LEFT_HAND, DISPATCHER_PROPERTIES
2017-08-23 17:32:41 -07:00
*/
Script.include("/~/system/libraries/controllerDispatcherUtils.js");
2017-08-23 17:32:41 -07:00
Script.include("/~/system/libraries/controllers.js");
(function() {
2018-12-10 15:07:08 -08:00
const intersectionType = {
None: 0,
WebOverlay: 1,
WebEntity: 2,
HifiKeyboard: 3,
Overlay: 4,
HifiTablet: 5,
};
2017-11-14 13:04:09 -08:00
function WebSurfaceLaserInput(hand) {
2017-08-23 17:32:41 -07:00
this.hand = hand;
this.otherHand = this.hand === RIGHT_HAND ? LEFT_HAND : RIGHT_HAND;
2017-11-13 13:37:53 -08:00
this.running = false;
2018-10-10 14:08:47 -07:00
this.ignoredObjects = [];
2018-12-10 15:07:08 -08:00
this.intersectedType = intersectionType["None"];
2017-08-23 17:32:41 -07:00
this.parameters = makeDispatcherModuleParameters(
160,
2017-08-23 17:32:41 -07:00
this.hand === RIGHT_HAND ? ["rightHand"] : ["leftHand"],
[],
2017-10-31 14:13:42 -07:00
100,
2017-11-28 15:37:36 -08:00
makeLaserParams(hand, true));
2017-08-23 17:32:41 -07:00
this.getFarGrab = function () {
return getEnabledModuleByName(this.hand === RIGHT_HAND ? ("RightFarGrabEntity") : ("LeftFarGrabEntity"));
};
this.farGrabActive = function () {
var farGrab = this.getFarGrab();
// farGrab will be null if module isn't loaded.
if (farGrab) {
return farGrab.targetIsNull();
} else {
return false;
}
};
2017-09-20 14:10:10 -07:00
this.grabModuleWantsNearbyOverlay = function(controllerData) {
if (controllerData.triggerValues[this.hand] > TRIGGER_ON_VALUE || controllerData.secondaryValues[this.hand] > BUMPER_ON_VALUE) {
2017-09-20 14:10:10 -07:00
var nearGrabName = this.hand === RIGHT_HAND ? "RightNearParentingGrabOverlay" : "LeftNearParentingGrabOverlay";
var nearGrabModule = getEnabledModuleByName(nearGrabName);
if (nearGrabModule) {
var candidateOverlays = controllerData.nearbyOverlayIDs[this.hand];
var grabbableOverlays = candidateOverlays.filter(function(overlayID) {
//V8TODO: this needs to be checked if it works
return Entities.getEntityProperties(overlayID, ["grab"]).grab.grabbable;
2017-09-20 14:10:10 -07:00
});
var target = nearGrabModule.getTargetID(grabbableOverlays, controllerData);
if (target) {
return true;
}
}
nearGrabName = this.hand === RIGHT_HAND ? "RightNearParentingGrabEntity" : "LeftNearParentingGrabEntity";
nearGrabModule = getEnabledModuleByName(nearGrabName);
if (nearGrabModule && nearGrabModule.isReady(controllerData)) {
// check for if near parent module is active.
var isNearGrabModuleActive = nearGrabModule.isReady(controllerData).active;
if (isNearGrabModuleActive) {
// if true, return true.
return isNearGrabModuleActive;
} else {
// check near action grab entity as a second pass.
nearGrabName = this.hand === RIGHT_HAND ? "RightNearActionGrabEntity" : "LeftNearActionGrabEntity";
nearGrabModule = getEnabledModuleByName(nearGrabName);
if (nearGrabModule && nearGrabModule.isReady(controllerData)) {
return nearGrabModule.isReady(controllerData).active;
}
}
}
2017-09-20 14:10:10 -07:00
}
var nearTabletHighlightModule = getEnabledModuleByName(this.hand === RIGHT_HAND
? "RightNearTabletHighlight" : "LeftNearTabletHighlight");
if (nearTabletHighlightModule) {
return nearTabletHighlightModule.isNearTablet(controllerData);
}
2017-09-20 14:10:10 -07:00
return false;
};
2017-11-13 13:37:53 -08:00
this.getOtherModule = function() {
return this.hand === RIGHT_HAND ? leftOverlayLaserInput : rightOverlayLaserInput;
};
2018-10-10 14:08:47 -07:00
this.addObjectToIgnoreList = function(controllerData) {
if (Window.interstitialModeEnabled && !Window.isPhysicsEnabled()) {
var intersection = controllerData.rayPicks[this.hand];
var objectID = intersection.objectID;
if (intersection.type === Picks.INTERSECTED_OVERLAY) {
2018-10-10 14:08:47 -07:00
var overlayIndex = this.ignoredObjects.indexOf(objectID);
var overlayName = Entities.getEntityProperties(objectID, ["name"]).name;
2018-10-10 14:08:47 -07:00
if (overlayName !== "Loading-Destination-Card-Text" && overlayName !== "Loading-Destination-Card-GoTo-Image" &&
overlayName !== "Loading-Destination-Card-GoTo-Image-Hover") {
var data = {
action: 'add',
id: objectID
};
Messages.sendMessage('Hifi-Hand-RayPick-Blacklist', JSON.stringify(data));
this.ignoredObjects.push(objectID);
}
2018-10-10 14:08:47 -07:00
} else if (intersection.type === Picks.INTERSECTED_ENTITY) {
var entityIndex = this.ignoredObjects.indexOf(objectID);
var data = {
action: 'add',
id: objectID
};
Messages.sendMessage('Hifi-Hand-RayPick-Blacklist', JSON.stringify(data));
this.ignoredObjects.push(objectID);
}
}
};
2018-10-10 14:08:47 -07:00
this.restoreIgnoredObjects = function() {
for (var index = 0; index < this.ignoredObjects.length; index++) {
var data = {
action: 'remove',
2018-10-10 14:08:47 -07:00
id: this.ignoredObjects[index]
};
Messages.sendMessage('Hifi-Hand-RayPick-Blacklist', JSON.stringify(data));
}
2018-10-12 12:07:39 -07:00
this.ignoredObjects = [];
};
2018-12-12 13:26:35 -08:00
this.getInteractableType = function(controllerData, triggerPressed, checkEntitiesOnly) {
2018-07-06 17:02:41 -07:00
// allow pointing at tablet, unlocked web entities, or web overlays automatically without pressing trigger,
// but for pointing at locked web entities or non-web overlays user must be pressing trigger
2017-08-25 17:40:30 -07:00
var intersection = controllerData.rayPicks[this.hand];
var objectID = intersection.objectID;
if (intersection.type === Picks.INTERSECTED_OVERLAY && !checkEntitiesOnly) {
2018-06-18 11:06:33 -07:00
if ((HMD.tabletID && objectID === HMD.tabletID) ||
2018-08-21 14:04:29 -07:00
(HMD.tabletScreenID && objectID === HMD.tabletScreenID) ||
2018-06-14 17:49:02 -07:00
(HMD.homeButtonID && objectID === HMD.homeButtonID)) {
2018-12-10 15:07:08 -08:00
return intersectionType["HifiTablet"];
2018-06-14 17:49:02 -07:00
} else {
var overlayType = Overlays.getOverlayType(objectID);
2018-12-10 15:07:08 -08:00
var type = intersectionType["None"];
if (Keyboard.containsID(objectID) && !Keyboard.preferMalletsOverLasers) {
type = intersectionType["HifiKeyboard"];
} else if (overlayType === "web3d") {
type = intersectionType["WebOverlay"];
} else if (triggerPressed) {
type = intersectionType["Overlay"];
}
return type;
2018-06-14 17:49:02 -07:00
}
} else if (intersection.type === Picks.INTERSECTED_ENTITY) {
var entityProperties = Entities.getEntityProperties(objectID, DISPATCHER_PROPERTIES);
var entityType = entityProperties.type;
var isLocked = entityProperties.locked;
2018-12-10 15:07:08 -08:00
if (entityType === "Web" && (!isLocked || triggerPressed)) {
return intersectionType["WebEntity"];
}
2017-09-22 13:19:52 -07:00
}
2018-12-10 15:07:08 -08:00
return intersectionType["None"];
};
this.deleteContextOverlay = function() {
var farGrabModule = getEnabledModuleByName(this.hand === RIGHT_HAND ?
"RightFarActionGrabEntity" :
"LeftFarActionGrabEntity");
if (farGrabModule) {
var entityWithContextOverlay = farGrabModule.entityWithContextOverlay;
if (entityWithContextOverlay) {
ContextOverlay.destroyContextOverlay(entityWithContextOverlay);
farGrabModule.entityWithContextOverlay = false;
}
}
};
2018-12-10 15:07:08 -08:00
this.updateAlwaysOn = function(type) {
2018-01-04 15:00:17 +13:00
var PREFER_STYLUS_OVER_LASER = "preferStylusOverLaser";
2018-12-12 13:26:35 -08:00
this.parameters.handLaser.alwaysOn = (!Settings.getValue(PREFER_STYLUS_OVER_LASER, false) || type === intersectionType["HifiKeyboard"]);
2018-01-04 15:00:17 +13:00
};
2018-01-05 11:04:09 +13:00
this.getDominantHand = function() {
return MyAvatar.getDominantHand() === "right" ? 1 : 0;
};
this.dominantHandOverride = false;
2018-01-18 22:31:12 +13:00
this.isReady = function (controllerData) {
// Trivial rejection for when FarGrab is active.
if (this.farGrabActive()) {
return makeRunningValues(false, [], []);
}
var isTriggerPressed = controllerData.triggerValues[this.hand] > TRIGGER_OFF_VALUE &&
2018-12-12 13:26:35 -08:00
controllerData.triggerValues[this.otherHand] <= TRIGGER_OFF_VALUE;
var type = this.getInteractableType(controllerData, isTriggerPressed, false);
2018-12-10 15:07:08 -08:00
if (type !== intersectionType["None"] && !this.grabModuleWantsNearbyOverlay(controllerData)) {
if (type === intersectionType["WebOverlay"] || type === intersectionType["WebEntity"] || type === intersectionType["HifiTablet"]) {
var otherModuleRunning = this.getOtherModule().running;
otherModuleRunning = otherModuleRunning && this.getDominantHand() !== this.hand; // Auto-swap to dominant hand.
var allowThisModule = !otherModuleRunning || isTriggerPressed;
if (!allowThisModule) {
return makeRunningValues(true, [], []);
}
if (isTriggerPressed) {
this.dominantHandOverride = true; // Override dominant hand.
this.getOtherModule().dominantHandOverride = false;
}
}
2018-12-10 15:07:08 -08:00
this.updateAlwaysOn(type);
2018-12-12 13:26:35 -08:00
if (this.parameters.handLaser.alwaysOn || isTriggerPressed) {
return makeRunningValues(true, [], []);
2018-01-04 15:00:17 +13:00
}
2017-08-25 14:20:31 -07:00
}
2018-10-10 14:08:47 -07:00
if (Window.interstitialModeEnabled && Window.isPhysicsEnabled()) {
this.restoreIgnoredObjects();
}
2017-08-25 14:20:31 -07:00
return makeRunningValues(false, [], []);
};
2017-08-24 17:42:27 -07:00
2018-12-10 15:07:08 -08:00
this.shouldThisModuleRun = function(controllerData) {
var otherModuleRunning = this.getOtherModule().running;
otherModuleRunning = otherModuleRunning && this.getDominantHand() !== this.hand; // Auto-swap to dominant hand.
otherModuleRunning = otherModuleRunning || this.getOtherModule().dominantHandOverride; // Override dominant hand.
var grabModuleNeedsToRun = this.grabModuleWantsNearbyOverlay(controllerData);
2018-09-11 15:23:40 -07:00
// only allow for non-near grab
2018-12-10 15:07:08 -08:00
return !otherModuleRunning && !grabModuleNeedsToRun;
};
this.run = function(controllerData, deltaTime) {
this.addObjectToIgnoreList(controllerData);
2018-06-14 17:49:02 -07:00
var isTriggerPressed = controllerData.triggerValues[this.hand] > TRIGGER_OFF_VALUE;
2019-03-07 13:11:18 -08:00
var type = this.getInteractableType(controllerData, isTriggerPressed, false);
2018-12-12 13:26:35 -08:00
var laserOn = isTriggerPressed || this.parameters.handLaser.alwaysOn;
2018-10-10 14:08:47 -07:00
this.addObjectToIgnoreList(controllerData);
2018-12-10 15:07:08 -08:00
if (type === intersectionType["HifiTablet"] && laserOn) {
if (this.shouldThisModuleRun(controllerData)) {
this.running = true;
return makeRunningValues(true, [], []);
2018-12-10 15:07:08 -08:00
}
} else if ((type === intersectionType["WebOverlay"] || type === intersectionType["WebEntity"]) && laserOn) { // auto laser on WebEntities andWebOverlays
if (this.shouldThisModuleRun(controllerData)) {
2018-09-11 15:23:40 -07:00
this.running = true;
return makeRunningValues(true, [], []);
}
2018-12-10 15:07:08 -08:00
} else if ((type === intersectionType["HifiKeyboard"] && laserOn) || type === intersectionType["Overlay"]) {
this.running = true;
return makeRunningValues(true, [], []);
}
2018-12-10 15:07:08 -08:00
this.deleteContextOverlay();
2017-11-13 13:37:53 -08:00
this.running = false;
this.dominantHandOverride = false;
return makeRunningValues(false, [], []);
2017-08-23 17:32:41 -07:00
};
2017-08-31 18:06:55 -07:00
}
2017-08-25 14:20:31 -07:00
2017-11-14 13:04:09 -08:00
var leftOverlayLaserInput = new WebSurfaceLaserInput(LEFT_HAND);
var rightOverlayLaserInput = new WebSurfaceLaserInput(RIGHT_HAND);
2017-08-23 17:32:41 -07:00
2017-11-14 13:04:09 -08:00
enableDispatcherModule("LeftWebSurfaceLaserInput", leftOverlayLaserInput);
enableDispatcherModule("RightWebSurfaceLaserInput", rightOverlayLaserInput);
2017-08-23 17:32:41 -07:00
2017-10-31 14:13:42 -07:00
function cleanup() {
2017-11-14 13:04:09 -08:00
disableDispatcherModule("LeftWebSurfaceLaserInput");
disableDispatcherModule("RightWebSurfaceLaserInput");
2017-11-01 11:36:36 -07:00
}
2017-10-31 14:13:42 -07:00
Script.scriptEnding.connect(cleanup);
2017-08-23 17:32:41 -07:00
}());