181 lines
6.7 KiB
JavaScript
Raw Permalink Normal View History

2018-01-07 22:14:15 -08:00
"use strict";
//
// emote.js
// scripts/system/
//
// Created by Brad Hefta-Gaub on 7 Jan 2018
// 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
//
/* globals Script, Tablet */
/* eslint indent: ["error", 4, { "outerIIFEBody": 0 }] */
(function() { // BEGIN LOCAL_SCOPE
var controllerStandard = Controller.Standard;
2018-01-07 22:14:15 -08:00
2018-06-26 15:02:46 -07:00
var EMOTE_ANIMATIONS =
['Crying', 'Surprised', 'Dancing', 'Cheering', 'Waving', 'Fall', 'Pointing', 'Clapping', 'Sit1', 'Sit2', 'Sit3', 'Love'];
2018-01-07 22:14:15 -08:00
var ANIMATIONS = Array();
var eventMappingName = "io.highfidelity.away"; // restoreAnimation on hand controller button events, too
var eventMapping = Controller.newMapping(eventMappingName);
2018-01-07 22:14:15 -08:00
EMOTE_ANIMATIONS.forEach(function (name) {
2018-01-09 13:42:51 -08:00
var animationURL = Script.resolvePath("assets/animations/" + name + ".fbx");
var resource = AnimationCache.prefetch(animationURL);
var animation = AnimationCache.getAnimation(animationURL);
ANIMATIONS[name] = { url: animationURL, animation: animation, resource: resource};
2018-01-07 22:14:15 -08:00
});
var EMOTE_APP_BASE = "html/EmoteApp.html";
var EMOTE_APP_URL = Script.resolvePath(EMOTE_APP_BASE);
2018-01-09 13:42:51 -08:00
var EMOTE_LABEL = "EMOTE";
var EMOTE_APP_SORT_ORDER = 12;
2018-01-08 17:32:22 -08:00
var FPS = 60;
2018-01-08 08:55:32 -08:00
var MSEC_PER_SEC = 1000;
2018-06-26 14:21:10 -07:00
var FINISHED = 3; // see ScriptableResource::State
2018-01-07 22:14:15 -08:00
var onEmoteScreen = false;
var button;
var tablet = Tablet.getTablet("com.highfidelity.interface.tablet.system");
var activeTimer = false; // Used to cancel active timer if a user plays an animation while another animation is playing
var activeEmote = false; // To keep track of the currently playing emote
2018-01-07 22:14:15 -08:00
button = tablet.addButton({
icon: "icons/tablet-icons/emote-i.svg",
activeIcon: "icons/tablet-icons/emote-a.svg",
2018-01-09 13:42:51 -08:00
text: EMOTE_LABEL,
2018-01-08 08:55:32 -08:00
sortOrder: EMOTE_APP_SORT_ORDER
2018-01-07 22:14:15 -08:00
});
function onClicked() {
if (onEmoteScreen) {
tablet.gotoHomeScreen();
} else {
onEmoteScreen = true;
tablet.gotoWebScreen(EMOTE_APP_URL);
}
}
function onScreenChanged(type, url) {
onEmoteScreen = type === "Web" && (url.indexOf(EMOTE_APP_BASE) === url.length - EMOTE_APP_BASE.length);
2018-01-07 22:14:15 -08:00
button.editProperties({ isActive: onEmoteScreen });
}
// Handle the events we're receiving from the web UI
function onWebEventReceived(event) {
// Converts the event to a JavasScript Object
if (typeof event === "string") {
event = JSON.parse(event);
}
if (event.type === "click") {
2018-06-26 14:21:10 -07:00
// Allow for a random sitting animation when a user selects sit
var randSit = Math.floor(Math.random() * 3) + 1;
2018-01-07 22:14:15 -08:00
var emoteName = event.data;
2018-06-26 14:21:10 -07:00
if (emoteName === "Sit"){
2018-06-26 15:02:46 -07:00
emoteName = event.data + randSit; // Sit1, Sit2, Sit3
}
if (ANIMATIONS[emoteName].resource.state === FINISHED) {
2018-06-26 14:21:10 -07:00
if (activeTimer !== false) {
Script.clearTimeout(activeTimer);
2018-01-08 08:55:32 -08:00
}
2018-06-26 15:02:46 -07:00
// If the activeEmote is different from the chosen emote, then play the new emote
// This is a second click on the same emote as the activeEmote, and we will just stop it
2018-06-26 14:21:10 -07:00
if (activeEmote !== emoteName) {
activeEmote = emoteName;
2018-06-26 14:21:10 -07:00
// Sit is the only animation currently that plays and then ends at the last frame
if (emoteName.match(/^Sit.*$/)) {
2018-06-26 15:02:46 -07:00
// If user provides input during a sit, the avatar animation state should be restored
2018-06-26 14:21:10 -07:00
Controller.keyPressEvent.connect(restoreAnimation);
Controller.enableMapping(eventMappingName);
2018-06-26 14:21:10 -07:00
MyAvatar.overrideAnimation(ANIMATIONS[emoteName].url, FPS, false, 0, frameCount);
2018-06-26 14:21:10 -07:00
} else {
activeEmote = emoteName;
var frameCount = ANIMATIONS[emoteName].animation.frames.length;
MyAvatar.overrideAnimation(ANIMATIONS[emoteName].url, FPS, false, 0, frameCount);
var timeOut = MSEC_PER_SEC * frameCount / FPS;
activeTimer = Script.setTimeout(function () {
MyAvatar.restoreAnimation();
activeTimer = false;
activeEmote = false;
}, timeOut);
}
} else {
activeEmote = false;
MyAvatar.restoreAnimation();
}
2018-01-07 22:14:15 -08:00
}
}
}
2018-06-26 15:02:46 -07:00
// Restore the navigation animation states (idle, walk, run)
function restoreAnimation() {
MyAvatar.restoreAnimation();
2018-06-26 15:02:46 -07:00
// Make sure the input is disconnected after animations are restored so it doesn't affect any emotes other than sit
2018-06-26 14:21:10 -07:00
Controller.keyPressEvent.disconnect(restoreAnimation);
Controller.disableMapping(eventMappingName);
}
2018-06-26 14:21:10 -07:00
// Note peek() so as to not interfere with other mappings.
eventMapping.from(controllerStandard.LeftPrimaryThumb).peek().to(restoreAnimation);
eventMapping.from(controllerStandard.RightPrimaryThumb).peek().to(restoreAnimation);
eventMapping.from(controllerStandard.LeftSecondaryThumb).peek().to(restoreAnimation);
eventMapping.from(controllerStandard.RightSecondaryThumb).peek().to(restoreAnimation);
eventMapping.from(controllerStandard.LB).peek().to(restoreAnimation);
eventMapping.from(controllerStandard.LS).peek().to(restoreAnimation);
eventMapping.from(controllerStandard.RY).peek().to(restoreAnimation);
eventMapping.from(controllerStandard.RX).peek().to(restoreAnimation);
eventMapping.from(controllerStandard.LY).peek().to(restoreAnimation);
eventMapping.from(controllerStandard.LX).peek().to(restoreAnimation);
eventMapping.from(controllerStandard.LeftGrip).peek().to(restoreAnimation);
eventMapping.from(controllerStandard.RB).peek().to(restoreAnimation);
eventMapping.from(controllerStandard.RS).peek().to(restoreAnimation);
eventMapping.from(controllerStandard.RightGrip).peek().to(restoreAnimation);
eventMapping.from(controllerStandard.Back).peek().to(restoreAnimation);
eventMapping.from(controllerStandard.Start).peek().to(restoreAnimation);
2018-01-07 22:14:15 -08:00
button.clicked.connect(onClicked);
tablet.screenChanged.connect(onScreenChanged);
tablet.webEventReceived.connect(onWebEventReceived);
Script.scriptEnding.connect(function () {
if (onEmoteScreen) {
tablet.gotoHomeScreen();
}
button.clicked.disconnect(onClicked);
tablet.screenChanged.disconnect(onScreenChanged);
if (tablet) {
tablet.removeButton(button);
}
2018-01-08 08:55:32 -08:00
if (activeTimer !== false) {
Script.clearTimeout(activeTimer);
MyAvatar.restoreAnimation();
}
2018-01-07 22:14:15 -08:00
});
}()); // END LOCAL_SCOPE