content/hifi-content/luis/test_scripts/replicator_app/replicatorApp.js
2022-02-14 02:04:11 +01:00

127 lines
No EOL
3.7 KiB
JavaScript

//
// Created by Luis Cuenca on 5/24/18
// Copyright 2018 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
//
/* jslint bitwise: true */
/* global Script, MyAvatar, GlobalDebugger, Tablet
*/
(function(){
Script.registerValue("REPLICATORAPP", true);
var TABLET_BUTTON_NAME = "REPLICATOR";
var HTML_URL = Script.resolvePath("./replicatorApp.html");
var DOMAIN_TRANS = "file:///~/serverless/tutorial.json";
var DOMAIN_GOTO = "hifi://engine-dev";
var MSG_DOCUMENT_LOADED = 0;
var MSG_CREATE = 1;
var MSG_INIT = 2;
var MSG_STOP = 3;
var tablet = Tablet.getTablet("com.highfidelity.interface.tablet.system");
var tabletButton = tablet.addButton({
text: TABLET_BUTTON_NAME,
icon: Script.resolvePath("./replicator-i.svg"),
activeIcon: Script.resolvePath("./replicator-a.svg")
});
var shown = false;
function manageClick() {
if (shown) {
tablet.gotoHomeScreen();
} else {
tablet.gotoWebScreen(HTML_URL);
}
}
tabletButton.clicked.connect(manageClick);
function onScreenChanged(type, url) {
console.log("Screen changed");
if (type === "Web" && url === HTML_URL) {
tabletButton.editProperties({isActive: true});
if (!shown) {
// hook up to event bridge
tablet.webEventReceived.connect(onWebEventReceived);
}
shown = true;
} else {
tabletButton.editProperties({isActive: false});
if (shown) {
// disconnect from event bridge
tablet.webEventReceived.disconnect(onWebEventReceived);
}
shown = false;
}
}
function onWebEventReceived(msg) {
var message = JSON.parse(msg);
switch(message.type) {
case MSG_INIT: {
init(message.data);
break;
}
case MSG_STOP: {
stop();
break;
}
case MSG_DOCUMENT_LOADED: {
tablet.emitScriptEvent(JSON.stringify(
{
"type": MSG_CREATE,
"data": {
"replicaCount": Test.getOtherAvatarsReplicaCount(),
"domainTrans": DOMAIN_TRANS,
"domainGoto": DOMAIN_GOTO
}
}
));
break;
}
}
}
tablet.screenChanged.connect(onScreenChanged);
function shutdownTabletApp() {
tablet.removeButton(tabletButton);
if (shown) {
tablet.webEventReceived.disconnect(onWebEventReceived);
tablet.gotoHomeScreen();
}
tablet.screenChanged.disconnect(onScreenChanged);
}
function init(data) {
DOMAIN_GOTO = data.domainGoto;
DOMAIN_TRANS = data.domainTrans;
if (Window.location == DOMAIN_GOTO) {
stop();
}
Script.setTimeout(function() {
Test.setOtherAvatarsReplicaCount(data.replicaCount);
Window.location = DOMAIN_GOTO;
}, 500);
}
function stop() {
Test.setOtherAvatarsReplicaCount(0);
Window.location = DOMAIN_TRANS;
}
Script.scriptEnding.connect(function () {
shutdownTabletApp();
});
}());