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

128 lines
No EOL
3.7 KiB
JavaScript

//
// Created by Luis Cuenca on 9/13/19
// 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, AvatarManager, GlobalDebugger, Tablet
*/
(function(){
Script.registerValue("FIRSTPCAMERAAPP", true);
var TABLET_BUTTON_NAME = "FIRSTPCAM";
var HTML_URL = Script.resolvePath("./firstPApp.html");
var MSG_DOCUMENT_LOADED = 0;
var MSG_CREATE = 1;
var MSG_REFRESH = 2;
var MESSAGES_CHANNEL_REFRESH = "com.highfidelity.interface.fistPCameraApp:refresh";
Messages.subscribe(MESSAGES_CHANNEL_REFRESH);
var tablet = Tablet.getTablet("com.highfidelity.interface.tablet.system");
var tabletButton = tablet.addButton({
text: TABLET_BUTTON_NAME,
icon: Script.resolvePath("./lookAt-i.svg"),
activeIcon: Script.resolvePath("./lookAt-a.svg")
});
var globalData = MyAvatar.getLookAtCameraData();
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_REFRESH: {
refresh(message.data, msg);
break;
}
case MSG_DOCUMENT_LOADED: {
tablet.emitScriptEvent(JSON.stringify(
{
"type": MSG_CREATE,
"data": globalData
}
));
break;
}
}
}
tablet.screenChanged.connect(onScreenChanged);
function shutdownTabletApp() {
tablet.removeButton(tabletButton);
if (shown) {
tablet.webEventReceived.disconnect(onWebEventReceived);
tablet.gotoHomeScreen();
}
tablet.screenChanged.disconnect(onScreenChanged);
}
function setValues(data) {
MyAvatar.setLookAtCameraData(data);
return;
}
function refresh(data, msg) {
setValues(data);
Messages.sendMessage(MESSAGES_CHANNEL_REFRESH, msg);
}
Messages.messageReceived.connect(function(channel, message, senderID) {
if (channel === MESSAGES_CHANNEL_REFRESH) {
console.log("Message received");
globalData = JSON.parse(message).data;
setValues(globalData);
if (globalData) {
tablet.emitScriptEvent(JSON.stringify(
{
"type": MSG_CREATE,
"data": globalData
}
));
}
}
});
Script.scriptEnding.connect(function () {
shutdownTabletApp();
});
}());