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

183 lines
5 KiB
JavaScript

(function () {
var entityID;
var code;
var loggedIn;
var overlayID;
var Y_OFFSET = 1.5; // m
var REMOVE_OVERLAY_TIME = 10000;
var FETCHING_TEXT = "Fetching code...";
var ERROR_TEXT = "Error occured. Please click again!";
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"); }
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,
name: "PPS Code Overlay"
// parent: entityID
});
} 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);
}, REMOVE_OVERLAY_TIME);
},
removeOverlay: function () {
if (DEBUG) { print("remove overlay"); }
Overlays.deleteOverlay(overlayID);
overlayID = null;
},
unload: function () {
if (overlayID) {
this.removeOverlay();
}
}
}
return new CodeGetter();
});