(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 removeFlag = false; 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, Camera.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(); }, setTimeoutToRemoveOverlay: function () { if (removeFlag === false) { removeFlag = true; var _this = this; Script.setTimeout(function () { _this.removeOverlay(overlayID); if (interval) { Script.clearInterval(interval); } removeFlag = false; }, 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(); });