From 0f9c637c5a490dd43da16ca74dbfde34e80fd843 Mon Sep 17 00:00:00 2001 From: Zach Pomerantz Date: Thu, 16 Jun 2016 01:32:38 -0700 Subject: [PATCH 1/4] Wireframe a metadata overlay tutorial --- scripts/tutorials/getDomainMetadata.js | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 scripts/tutorials/getDomainMetadata.js diff --git a/scripts/tutorials/getDomainMetadata.js b/scripts/tutorials/getDomainMetadata.js new file mode 100644 index 0000000000..1c78d640e8 --- /dev/null +++ b/scripts/tutorials/getDomainMetadata.js @@ -0,0 +1,28 @@ +// +// Created by Zach Pomerantz on June 16, 2016. +// Copyright 2016 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 +// + +// Avoid polluting the namespace +// !function() { + var MetadataNotification = null; + + // Every time you enter a domain, display the domain's metadata + SomeEvent.connect(function() { + // Fetch the domain metadata from the directory + + // Display the fetched metadata in an overlay + + }); + + // Remove the overlay if the script is stopped + Script.scriptEnding.connect(teardown); + + function teardown() { + // Close the overlay, if it exists + + } +// }(); From aca3d4d90b0acd3522eeabad214152a48406ac0b Mon Sep 17 00:00:00 2001 From: Zach Pomerantz Date: Thu, 16 Jun 2016 15:22:34 -0700 Subject: [PATCH 2/4] add example script for metadata from metaverse --- scripts/tutorials/getDomainMetadata.js | 118 ++++++++++++++++++++++--- 1 file changed, 105 insertions(+), 13 deletions(-) diff --git a/scripts/tutorials/getDomainMetadata.js b/scripts/tutorials/getDomainMetadata.js index 1c78d640e8..0a6f742823 100644 --- a/scripts/tutorials/getDomainMetadata.js +++ b/scripts/tutorials/getDomainMetadata.js @@ -6,23 +6,115 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -// Avoid polluting the namespace -// !function() { - var MetadataNotification = null; +var SERVER = 'https://metaverse.highfidelity.com/api/v1'; - // Every time you enter a domain, display the domain's metadata - SomeEvent.connect(function() { - // Fetch the domain metadata from the directory +var OVERLAY = null; - // Display the fetched metadata in an overlay +// Every time you enter a domain, display the domain's metadata +location.hostChanged.connect(function(host) { + print('Detected host change:', host); - }); + // Fetch the domain ID from the metaverse + var placeData = request(SERVER + '/places/' + host); + if (!placeData) { return; } + var domainID = placeData.data.place.domain.id; + print('Domain ID:', domainID); - // Remove the overlay if the script is stopped - Script.scriptEnding.connect(teardown); + // Fetch the domain metadata from the metaverse + var domainData = request(SERVER + '/domains/' + domainID); + print(SERVER + '/domains/' + domainID); + if (!domainData) { return; } + var metadata = domainData.domain; + print('Domain metadata:', JSON.stringify(metadata)); - function teardown() { - // Close the overlay, if it exists + // Display the fetched metadata in an overlay + displayMetadata(host, metadata); +}); +Script.scriptEnding.connect(clearMetadata); + +function displayMetadata(place, metadata) { + clearMetadata(); + + var COLOR_TEXT = { red: 255, green: 255, blue: 255 }; + var COLOR_BACKGROUND = { red: 0, green: 0, blue: 0 }; + var MARGIN = 200; + var STARTING_OPACITY = 0.8; + var FADE_AFTER_SEC = 2; + var FADE_FOR_SEC = 4; + + var fade_per_sec = STARTING_OPACITY / FADE_FOR_SEC; + var properties = { + color: COLOR_TEXT, + alpha: STARTING_OPACITY, + backgroundColor: COLOR_BACKGROUND, + backgroundAlpha: STARTING_OPACITY, + font: { size: 24 }, + x: MARGIN, + y: MARGIN + }; + + // Center the overlay on the screen + properties.width = Window.innerWidth - MARGIN*2; + properties.height = Window.innerHeight - MARGIN*2; + + // Parse the metadata into text + parsed = [ 'Welcome to ' + place + '!',, ]; + if (metadata.description) { + parsed.push(description); } -// }(); + if (metadata.tags && metadata.tags.length) { + parsed.push('Tags: ' + metadata.tags.join(',')); + } + if (metadata.capacity) { + parsed.push('Capacity (max users): ' + metadata.capacity); + } + if (metadata.maturity) { + parsed.push('Maturity: ' + metadata.maturity); + } + if (metadata.hosts && metadata.hosts.length) { + parsed.push('Hosts: ' + metadata.tags.join(',')); + } + if (metadata.online_users) { + parsed.push('Users online: ' + metadata.online_users); + } + + properties.text = parsed.join('\n\n'); + + // Display the overlay + OVERLAY = Overlays.addOverlay('text', properties); + + // Fade out the overlay over 10 seconds + !function() { + var overlay = OVERLAY; + var alpha = STARTING_OPACITY; + + var fade = function() { + // Only fade so long as the same overlay is up + if (overlay == OVERLAY) { + alpha -= fade_per_sec / 10; + if (alpha <= 0) { + clearMetadata(); + } else { + Overlays.editOverlay(overlay, { alpha: alpha, backgroundAlpha: alpha }); + Script.setTimeout(fade, 100); + } + } + }; + Script.setTimeout(fade, FADE_AFTER_SEC * 1000); + }(); +} + +function clearMetadata() { + if (OVERLAY) { Overlays.deleteOverlay(OVERLAY); } +} + +// Request JSON from a url, synchronously +function request(url) { + var req = new XMLHttpRequest(); + req.responseType = 'json'; + req.open('GET', url, false); + req.send(); + return req.status == 200 ? req.response : null; +} + From c784fa42e99e6b2862ddf73f80a7ae9a040dc9c6 Mon Sep 17 00:00:00 2001 From: Zach Pomerantz Date: Tue, 21 Jun 2016 11:34:23 -0700 Subject: [PATCH 3/4] log failure to fetch metadata --- scripts/tutorials/getDomainMetadata.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/scripts/tutorials/getDomainMetadata.js b/scripts/tutorials/getDomainMetadata.js index 0a6f742823..8c347a09ae 100644 --- a/scripts/tutorials/getDomainMetadata.js +++ b/scripts/tutorials/getDomainMetadata.js @@ -16,14 +16,21 @@ location.hostChanged.connect(function(host) { // Fetch the domain ID from the metaverse var placeData = request(SERVER + '/places/' + host); - if (!placeData) { return; } + if (!placeData) { + print('Cannot find place name - abandoning metadata request for', host); + return; + + } var domainID = placeData.data.place.domain.id; print('Domain ID:', domainID); // Fetch the domain metadata from the metaverse var domainData = request(SERVER + '/domains/' + domainID); print(SERVER + '/domains/' + domainID); - if (!domainData) { return; } + if (!domainData) { + print('Cannot find domain data - abandoning metadata request for', domainID); + return; + } var metadata = domainData.domain; print('Domain metadata:', JSON.stringify(metadata)); From c8ead2ad048845e46f5d127c132c7239e7c4d011 Mon Sep 17 00:00:00 2001 From: Zach Pomerantz Date: Tue, 21 Jun 2016 13:28:15 -0700 Subject: [PATCH 4/4] conform to coding braces standard --- scripts/tutorials/getDomainMetadata.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/tutorials/getDomainMetadata.js b/scripts/tutorials/getDomainMetadata.js index 8c347a09ae..54c356ae7b 100644 --- a/scripts/tutorials/getDomainMetadata.js +++ b/scripts/tutorials/getDomainMetadata.js @@ -113,7 +113,9 @@ function displayMetadata(place, metadata) { } function clearMetadata() { - if (OVERLAY) { Overlays.deleteOverlay(OVERLAY); } + if (OVERLAY) { + Overlays.deleteOverlay(OVERLAY); + } } // Request JSON from a url, synchronously