content/hifi-content/liv/dev/LivestreamTablet/app.js
2022-02-14 02:04:11 +01:00

232 lines
No EOL
7 KiB
JavaScript

(function () {
// System variables
var SPAWNERMODEL = "https://hifi-content.s3.amazonaws.com/liv/3DModels/Spawner.fbx";
var _spawnerObject = -1; // We set this when we have an object
var _username = GlobalServices.username; // Used to subscribe to Pub/Sub messages
var _interval;
// User specified variables
var _emojisToSpawn = [];
var _lifetime = 60;
var _frequency = 30;
var _showUserNames = true;
// Set up the app
if (Settings.getValue("ACTIVE_BROADCAST_QUERY") === "") {
Settings.setValue("ACTIVE_BROADCAST_QUERY", false);
}
var APP_NAME = "CONFIG";
var APP_URL = Script.resolvePath("app.html?") + _username + "&" + Settings.getValue("ACTIVE_BROADCAST_QUERY");
var tablet = Tablet.getTablet("com.highfidelity.interface.tablet.system");
var button = tablet.addButton({
text: APP_NAME
});
function onClicked() {
tablet.gotoWebScreen(APP_URL);
}
button.clicked.connect(onClicked);
// Handle web events
function onWebEventReceived(event) {
if (typeof event === "string") {
print("Received event: " + event);
event = JSON.parse(event);
}
// Handle the submit / start button event
if (event.type === "submit") {
var data = event.data;
// Get the user settings
_emojisToSpawn = data.emojis;
_lifetime = data.lifetime;
_frequency = data.frequency;
_showUserNames = data.useNames;
if (_spawnerObject === -1) {
// The user has not set up an object, so make one
_spawnerObject = createObjectForEmojiLocation();
}
Settings.setValue("ACTIVE_BROADCAST_QUERY", true); // We are now listening for emojis
}
// Handle the creation of a location entity
if (event.type === "setLoc") {
// We do not already have an object spawning
if (_spawnerObject === -1) {
_spawnerObject = createObjectForEmojiLocation();
} else {
Entities.editEntity(_spawnerObject, { position: Vec3.sum(MyAvatar.position, Quat.getFront(MyAvatar.orientation)) });
}
}
// Handle stopping listening for emojis
if (event.type === "stop") {
Entities.deleteEntity(_spawnerObject);
_spawnerObject = -1;
Settings.setValue("ACTIVE_BROADCAST_QUERY", false); // We are no longer querying for emojis
Script.clearInterval(_interval)
}
if (event.type === "receivedMsg") {
print(JSON.stringify(event.data.message.text));
}
}
function createObjectForEmojiLocation() {
var properties = {
position: Vec3.sum(MyAvatar.position, Quat.getFront(MyAvatar.orientation)),
grabbable: true,
type: "Model",
modelURL: SPAWNERMODEL
}
return Entities.addEntity(properties);
}
tablet.webEventReceived.connect(onWebEventReceived);
// Clean up when done
function cleanup() {
if (_spawnerObject != -1) {
// We have an emoji spawner to clean up
_activelyListening = false;
Entities.deleteEntity(_spawnerObject);
// Disconnect any scripts that are setting up
Settings.setValue("ACTIVE_BROADCAST_QUERY", false); // We are no longer querying for emojis
}
tablet.removeButton(button);
}
Script.scriptEnding.connect(cleanup);
/**
* BEGIN TEMPORARY EMOJI SOLUTION
* TODO: REPLACE WITH LIBRARY PENDING FINAL EMOJI APP
*/
// Set up our emojis
var _emojis = [];
var _overlays = [];
var EMOJI_MODEL_URLS = ["https://hifi-content.s3.amazonaws.com/liv/3DModels/heart.fbx",
"https://hifi-content.s3.amazonaws.com/liv/3DModels/Flower.fbx",
"https://hifi-content.s3.amazonaws.com/liv/3DModels/poo.fbx",
"https://hifi-content.s3.amazonaws.com/liv/3DModels/pizza.fbx",
"https://hifi-content.s3.amazonaws.com/liv/3DModels/Pickle_Mat.fbx"];
var EMOJI_SCRIPT_URLS = ["https://hifi-content.s3.amazonaws.com/liv/dev/emojis/HeartEmoji.js",
"https://hifi-content.s3.amazonaws.com/liv/dev/emojis/Flowers.js",
"https://hifi-content.s3.amazonaws.com/liv/dev/emojis/Fart.js",
"https://hifi-content.s3.amazonaws.com/liv/dev/emojis/Edible.js",
"https://hifi-content.s3.amazonaws.com/liv/dev/emojis/Crunch.js"];
var EMOJI_DIMENSIONS = [{ x: 0.1768, y: .1529, z: .0817 }, { x: .3272, y: 0.0172, z: 0.3343 },
{ x: .1563, y: 0.1122, z: 0.1563 }, { x: .4397, y: 0.0148, z: 0.3080 },
{ x: .1134, y: 0.0452, z: 0.0725 }];
var FUNCTION_URL = "https://sendanentity.azurewebsites.net/api/HttpTriggerJS/";
var Create3DEmoji = function (emojiType, userName) {
print("Creating " + emojiType + " emoji");
var index = 0;
switch (emojiType) {
case "Heart":
index = 0;
break;
case "Flowers":
index = 1;
break;
case "Poo":
index = 2;
break;
case "Pizza":
index = 3;
break;
case "Pickle":
index = 4;
break;
default:
print("Unsupported emoji type");
break;
};
var emojiProperties = {
type: "Model",
name: userName,
modelURL: EMOJI_MODEL_URLS[index],
position: Entities.getEntityProperties(_spawnerObject).position,
dimensions: EMOJI_DIMENSIONS[index],
dynamic: true,
angularVelocity: { x: 0, y: 5, z: 0 },
angularDamping: .1,
velocity: { x: 0, y: -.2, z: 0 },
gravity: { x: 0, y: -.1, z: 0 },
linearDamping: 0,
lifetime: _lifetime,
grabbable: true,
collisionless: false,
script: EMOJI_SCRIPT_URLS[index],
shapeType: 'Box'
}
var newEmoji = Entities.addEntity(emojiProperties);
print(JSON.stringify(Entities.getEntityProperties(newEmoji).position))
_emojis.push(newEmoji);
var nameOverlayProperties = {
type: "Text",
parentID: newEmoji,
position: Entities.getEntityProperties(_spawnerObject, 'position'),
localPosition: { x: 0.0, y: 0.2, z: 0.0 },
dimensions: { x: .5, y: .1, z: 0.01 },
lineHeight: 0.065,
text: userName
}
if (_showUserNames) {
var textEntity = Entities.addEntity(nameOverlayProperties);
_overlays.push(textEntity);
}
};
function parseResponse(response) {
var _hearts = response.Heart._;
var _sentHeart = formatUsernameList(response.SentHeart._);
for (var i = 0; i < _hearts; ++i) {
Create3DEmoji("Heart", _sentHeart[i]);
}
var _flowers = response.Flowers._;
var _sentFlowers = formatUsernameList(response.SentFlowers._);
for (var i = 0; i < _flowers; ++i) {
Create3DEmoji("Flowers", _sentFlowers[i]);
}
var _pizza = response.Pizza._;
var _sentPizza = formatUsernameList(response.SentPizza._);
for (var i = 0; i < _pizza; ++i) {
Create3DEmoji("Pizza", _sentPizza[i]);
}
var _poo = response.Poo._;
var _sentPoo = formatUsernameList(response.SentPoo._);
for (var i = 0; i < _poo; ++i) {
Create3DEmoji("Poo", _sentPoo[i]);
}
var _pickle = response.Pickle._;
var _sentPickle = formatUsernameList(response.SentPickle._);
for (var i = 0; i < _pickle; ++i) {
Create3DEmoji("Pickle", _sentPickle[i]);
}
};
// Helper function to format user names
function formatUsernameList(unformattedList) {
var trimmed = unformattedList.substring(2); // Remove prefixed comma
return trimmed.split(',');
};
/**
* END TEMPORARY EMOJI SOLUTION
*/
})();