overte/scripts/system/spectatorCamera.js
2017-06-06 11:59:22 -07:00

186 lines
5.2 KiB
JavaScript

"use strict";
/*jslint vars:true, plusplus:true, forin:true*/
/*global Tablet, Script, */
/* eslint indent: ["error", 4, { "outerIIFEBody": 0 }] */
//
// spectatorCamera.js
//
// Created by Zach Fox on 2017-06-05
// Copyright 2017 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
//
(function () { // BEGIN LOCAL_SCOPE
//
// FUNCTION VAR DECLARATIONS
//
var sendToQml, onTabletScreenChanged, fromQml, onTabletButtonClicked, wireEventBridge, startup, shutdown;
//
// Function Name: startup()
//
// Relevant Variables:
// button: The tablet button.
// buttonName: The name of the button.
// tablet: The tablet instance to be modified.
//
// Arguments:
// None
//
// Description:
// startup() will be called when the script is loaded.
//
var button;
var buttonName = "SPECTATOR";
var tablet = null;
function startup() {
tablet = Tablet.getTablet("com.highfidelity.interface.tablet.system");
button = tablet.addButton({
text: buttonName
});
button.clicked.connect(onTabletButtonClicked);
tablet.screenChanged.connect(onTabletScreenChanged);
}
//
// Function Name: wireEventBridge()
//
// Relevant Variables:
// hasEventBridge: true/false depending on whether we've already connected the event bridge
//
// Arguments:
// on: Enable or disable the event bridge
//
// Description:
// Used to connect/disconnect the script's response to the tablet's "fromQml" signal.
//
var hasEventBridge = false;
function wireEventBridge(on) {
if (on) {
if (!hasEventBridge) {
tablet.fromQml.connect(fromQml);
hasEventBridge = true;
}
} else {
if (hasEventBridge) {
tablet.fromQml.disconnect(fromQml);
hasEventBridge = false;
}
}
}
//
// Function Name: onTabletButtonClicked()
//
// Relevant Variables:
// onSpectatorCameraScreen: true/false depending on whether we're looking at the spectator camera app
// shouldActivateButton: true/false depending on whether we should show the button as white or gray the
// next time we edit the button's properties
//
// Arguments:
// None
//
// Description:
// Fired when the Spectator Camera app button is pressed.
//
var onSpectatorCameraScreen = false;
var shouldActivateButton = false;
function onTabletButtonClicked() {
if (onSpectatorCameraScreen) {
// for toolbar-mode: go back to home screen, this will close the window.
tablet.gotoHomeScreen();
} else {
shouldActivateButton = true;
tablet.loadQMLSource("../SpectatorCamera.qml");
onSpectatorCameraScreen = true;
}
}
//
// Function Name: onTabletScreenChanged()
//
// Relevant Variables:
// None
//
// Arguments:
// type: "Home", "Web", "Menu", "QML", "Closed"
// url: Only valid for Web and QML.
//
// Description:
// Called when the TabletScriptingInterface::screenChanged() signal is emitted.
//
function onTabletScreenChanged(type, url) {
wireEventBridge(shouldActivateButton);
// for toolbar mode: change button to active when window is first openend, false otherwise.
button.editProperties({ isActive: shouldActivateButton });
shouldActivateButton = false;
onSpectatorCameraScreen = false;
}
//
// Function Name: sendToQml()
//
// Relevant Variables:
// None
//
// Arguments:
// message: The message to send to the SpectatorCamera QML.
// Messages are in format "{method, params}", like json-rpc. See also fromQml().
//
// Description:
// Use this function to send a message to the QML (i.e. to change appearances).
//
function sendToQml(message) {
tablet.sendToQml(message);
}
//
// Function Name: fromQml()
//
// Relevant Variables:
// None
//
// Arguments:
// message: The message sent from the SpectatorCamera QML.
// Messages are in format "{method, params}", like json-rpc. See also sendToQml().
//
// Description:
// Called when a message is received from SpectatorCamera.qml.
//
function fromQml(message) {
switch (message.method) {
case 'XXX':
break;
default:
print('Unrecognized message from SpectatorCamera.qml:', JSON.stringify(message));
}
}
//
// Function Name: shutdown()
//
// Relevant Variables:
// None
//
// Arguments:
// None
//
// Description:
// shutdown() will be called when the script ends (i.e. is stopped).
//
function shutdown() {
tablet.removeButton(button);
button.clicked.disconnect(onTabletButtonClicked);
tablet.screenChanged.disconnect(onTabletScreenChanged);
}
//
// These functions will be called when the script is loaded.
//
startup();
Script.scriptEnding.connect(shutdown);
}()); // END LOCAL_SCOPE