content/hifi-content/Experiences/LoadTest/ppsCodeGenerator/v3/client_codeGetter.js
2022-02-13 23:16:46 +01:00

230 lines
6.6 KiB
JavaScript

(function () {
var entityID;
var code;
var loggedIn;
var overlayID;
var overlayID2;
var interval;
var Y_OFFSET = 1.5; // m
var REMOVE_OVERLAY_TIME = 10000;
var CODE = "L M B I";
var INSTRUCTIONS_URL = "https://hifi-content.s3.amazonaws.com/alan/dev/PCC-Code-Message.fbx";
var NOT_LOGGED_IN_TEXT = "You must be logged in!";
var CODE_DIMENSIONS = {x: 0.6, y: 0.3, z: 0.1};
var TEXT_DIMENSIONS = {x: 1.2, y: 0.3, z: 0.1};
var DEBUG = false;
function CodeGetter () {}
CodeGetter.prototype = {
remotelyCallable: [
"displayCode",
"displayError"
],
preload: function (id) {
entityID = id;
},
startNearTrigger: function () {
if (DEBUG) { print("startNearTrigger"); }
this.startGetCode();
},
startFarTrigger: function () {
if (DEBUG) { print("startFarTrigger"); }
this.startGetCode();
},
clickReleaseOnEntity: function (id, pointerEvent) {
if (DEBUG) { print("clickReleaseOnEntity"); }
if (pointerEvent.isPrimaryButton) {
this.startGetCode();
}
},
startGetCode: function () {
if (DEBUG) { print("startGetCode"); }
loggedIn = AccountServices.loggedIn;
if (!loggedIn) {
this.createOverlay(NOT_LOGGED_IN_TEXT);
this.handleNotLoggedIn();
return;
}
if (DEBUG) { print("yes logged in"); }
// var uuid = MyAvatar.sessionUUID;
// var username = AccountServices.username;
// if (!code) {
if (DEBUG) { print("calling getCode"); }
code = CODE;
this.createOverlay(CODE);
// Entities.callEntityServerMethod(entityID, "getCode", [uuid, username]);
// }
// var text = code ? code : FETCHING_TEXT;
// this.createOverlay(text);
},
handleNotLoggedIn: function () {
if (DEBUG) { print("handle not logged in"); }
var _this = this;
if (!setupNotLoggedIn) {
AccountServices.loggedInChanged.connect(loggedIn);
}
function loggedIn() {
AccountServices.loggedInChanged.disconnect(loggedIn);
_this.startGetCode();
}
},
createOverlay: function (text) {
if (DEBUG) { print("create overlay"); }
if (!overlayID) {
var properties = Entities.getEntityProperties(entityID, ["position"]);
var position = properties.position;
var overlayPosition = position;
position.y = position.y + Y_OFFSET;
var dimensions = code === text ? CODE_DIMENSIONS : TEXT_DIMENSIONS;
overlayID = Overlays.addOverlay("text3d", {
position: overlayPosition,
isFacingAvatar: true,
dimensions: dimensions,
lineHeight: 0.15,
text: text,
backgroundAlpha: 0.3,
name: "PPS Code Overlay"
// parent: entityID
});
var positionInstructions = {
x: overlayPosition.x,
y: overlayPosition.y + 0.2,
z: overlayPosition.z
};
if (code) {
overlayID2 = Overlays.addOverlay("model", {
position: positionInstructions,
url: INSTRUCTIONS_URL,
isFacingAvatar: true,
dimensions: {
x: 1.0939,
y: 0.7933,
z: 0.0010
},
name: "PPS Code Overlay Instructions"
})
}
if (!interval) {
var interval = Script.setInterval(function () {
Overlays.editOverlay(overlayID2, {
rotation: Quat.cancelOutRollAndPitch(Quat.lookAtSimple(positionInstructions, MyAvatar.position))
})
}, 50);
}
} else {
Overlays.editOverlay(overlayID, {
text: text
})
}
if (code) {
this.setTimeoutToRemoveOverlay();
}
},
// called remotely
displayCode: function (sessionUUID, param) {
if (DEBUG) { print("display code", sessionUUID, typeof param); }
code = param[0].split("").join(" ");
if (overlayID) {
Overlays.editOverlay(overlayID, {
text: code,
dimensions: CODE_DIMENSIONS
})
}
this.setTimeoutToRemoveOverlay();
},
// called remotely
displayError: function (sessionUUID, entityID) {
if (DEBUG) { print("display error"); }
var errorText = ERROR_TEXT;
if (overlayID) {
Overlays.editOverlay(overlayID, {
text: errorText
})
} else {
this.createOverlay(errorText);
}
this.setTimeoutToRemoveOverlay();
},
setTimeoutToRemoveOverlay: function () {
var _this = this;
Script.setTimeout(function () {
_this.removeOverlay(overlayID);
if (interval) {
Script.clearInterval(interval);
}
}, REMOVE_OVERLAY_TIME);
},
removeOverlay: function () {
if (DEBUG) { print("remove overlay"); }
Overlays.deleteOverlay(overlayID);
overlayID = null;
if (overlayID2) {
Overlays.deleteOverlay(overlayID2);
overlayID2 = null;
}
},
unload: function () {
if (overlayID) {
this.removeOverlay();
}
if (overlayID2) {
Overlays.deleteOverlay(overlayID2);
overlayID2 = null;
}
}
}
return new CodeGetter();
});