content/hifi-content/rebecca/laser/laserApp.js
2022-02-14 02:04:11 +01:00

138 lines
4.8 KiB
JavaScript

//
// laserApp.js
//
// Created by Rebecca Stankus on 06/11/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
(function() {
print("starting stageapp");
var LASER_BUTTON_IMAGE = Script.resolvePath('svg/laser-pointer-i.svg?3');
var LASER_BUTTON_PRESSED = Script.resolvePath('svg/laser-pointer-a.svg?3');
var WINDOW_Y_OFFSET = 24;
var BUTTON_DIMENSIONS = {x: 221, y: 69};
var tablet = Tablet.getTablet('com.highfidelity.interface.tablet.system');
var appPage = Script.resolvePath('laser.html?1');
var button = tablet.addButton({
text: 'LASER',
icon: LASER_BUTTON_IMAGE,
activeIcon: LASER_BUTTON_PRESSED
});
var open = false;
var laserOverlay;
var previousHandLocations = [];
var previousHandOrientations = [];
// Tablet Handlers
function onClicked() {
if (open) {
tablet.gotoHomeScreen();
} else {
tablet.gotoWebScreen(appPage);
}
}
function onWebEventReceived(event) {
if (typeof event === 'string') {
event = JSON.parse(event);
}
if (event.type === 'toggle') {
// playIndividualClapSound();
}
if (event.type === 'flashlight') {
// if (event.value === 'start') {
// startContinuousApplause();
// } else {
// stopContinuousApplause();
// }
}
if (event.type === 'done') {
// var position = JSON.stringify(MyAvatar.position);
// var applauseServerEntity = Entities.findEntitiesByName(SERVER_ENTITY_NAME, position, 15)[0]; // find first result
// Entities.callEntityServerMethod(applauseServerEntity, 'playIndividualClap', [position]);
}
}
var mousePressEvent = function (event) {
if (!event.isLeftButton) {
return;
}
var selectedResult = Overlays.getOverlayAtPoint({x: event.x, y: event.y});
if (selectedResult === laserOverlay) {
Overlays.editOverlay(laserOverlay, { imageURL: LASER_BUTTON_PRESSED});
Script.setTimeout(function() {
Overlays.editOverlay(laserOverlay, { imageURL: LASER_BUTTON_IMAGE});
}, 150);
// playIndividualClapSound();
}
};
function onScreenChanged(type, url) {
open = (url === appPage);
button.editProperties({isActive: open});
if (open) {
if (HMD.active) {
// Script.update.connect(checkHandsDistance);
// checkDistanceActive = true;
}
previousHandLocations.push(MyAvatar.getJointPosition('RightHand'));
previousHandLocations.push(MyAvatar.getJointPosition('LeftHand'));
previousHandOrientations.push(MyAvatar.getRightPalmRotation());
previousHandOrientations.push(MyAvatar.getLeftPalmRotation());
if (!HMD.active) {
// needsCleanup = true;
Controller.mousePressEvent.connect(mousePressEvent);
addDesktopOverlay();
}
} else {
// if (checkDistanceActive === true) {
// Script.update.disconnect(checkHandsDistance);
// checkDistanceActive = false;
// }
// if (needsCleanup) {
// Controller.mousePressEvent.disconnect(mousePressEvent);
// removeDesktopOverlay();
// }
}
}
function appEnding() {
button.clicked.disconnect(onClicked);
tablet.removeButton(button);
tablet.screenChanged.disconnect(onScreenChanged);
tablet.webEventReceived.disconnect(onWebEventReceived);
Controller.mousePressEvent.disconnect(mousePressEvent);
removeDesktopOverlay();
}
button.clicked.connect(onClicked);
tablet.screenChanged.connect(onScreenChanged);
tablet.webEventReceived.connect(onWebEventReceived);
Script.scriptEnding.connect(appEnding);
function removeDesktopOverlay() {
Overlays.deleteOverlay(laserOverlay);
}
function addDesktopOverlay() {
removeDesktopOverlay();
var windowHeight = Controller.getViewportDimensions().y;
laserOverlay = Overlays.addOverlay('image', {
imageURL: LASER_BUTTON_IMAGE,
x: 0,
y: windowHeight - BUTTON_DIMENSIONS.y - WINDOW_Y_OFFSET,
width: BUTTON_DIMENSIONS.x,
height: BUTTON_DIMENSIONS.y,
alpha: 1.0,
visible: true
});
}
}());