content/hifi-content/luis/solidHandsApp/solidhandApp.js
2022-02-14 02:04:11 +01:00

113 lines
No EOL
3.2 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("SOLIDHANDAPP", true);
var TABLET_BUTTON_NAME = "SOLIDHAND";
var HTML_URL = Script.resolvePath("./solidhandApp.html");
var MSG_DOCUMENT_LOADED = 0;
var MSG_COLLISION_DATA = 1;
var MSG_CREATE = 2;
var MSG_RESET_VALUES = 3;
var tablet = Tablet.getTablet("com.highfidelity.interface.tablet.system");
var tabletButton = tablet.addButton({
text: TABLET_BUTTON_NAME,
icon: Script.resolvePath("./solidhand-i.svg"),
activeIcon: Script.resolvePath("./solidhand-a.svg")
});
var shown = false;
function manageClick() {
if (shown) {
tablet.gotoHomeScreen();
} else {
tablet.gotoWebScreen(HTML_URL);
}
}
tabletButton.clicked.connect(manageClick);
MyAvatar.skeletonChanged.connect(function(){
if (shown) {
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_COLLISION_DATA: {
MyAvatar.updateDetailedPhysics(message.data);
break;
}
case MSG_DOCUMENT_LOADED: {
tablet.emitScriptEvent(JSON.stringify(
{
"type": MSG_CREATE,
"data": MyAvatar.getDetailedPhysics()
}
));
break;
}
case MSG_RESET_VALUES: {
tablet.emitScriptEvent(JSON.stringify(
{
"type": MSG_CREATE,
"data": MyAvatar.getDetailedPhysics(true)
}
));
break;
}
}
}
tablet.screenChanged.connect(onScreenChanged);
function shutdownTabletApp() {
tablet.removeButton(tabletButton);
if (shown) {
tablet.webEventReceived.disconnect(onWebEventReceived);
tablet.gotoHomeScreen();
}
tablet.screenChanged.disconnect(onScreenChanged);
}
Script.scriptEnding.connect(function () {
shutdownTabletApp();
});
}());