From 9bafd82b2c03c1f786785acfd4e485e0030d864b Mon Sep 17 00:00:00 2001 From: "Anthony J. Thibault" Date: Tue, 9 Aug 2016 15:49:00 -0700 Subject: [PATCH] marketplace now pops up a marketplace web entity --- scripts/system/libraries/WebBuddy.js | 95 ++++++++++++++++++++++++++++ scripts/system/marketplace.js | 37 ++++++++--- 2 files changed, 124 insertions(+), 8 deletions(-) create mode 100644 scripts/system/libraries/WebBuddy.js diff --git a/scripts/system/libraries/WebBuddy.js b/scripts/system/libraries/WebBuddy.js new file mode 100644 index 0000000000..d584bcc564 --- /dev/null +++ b/scripts/system/libraries/WebBuddy.js @@ -0,0 +1,95 @@ +// +// WebBuddy.js +// +// Created by Anthony J. Thibault on 8/8/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 +// +var NEGATIVE_ONE = 65535; + +// ctor +WebBuddy = function (url) { + + var ASPECT = 4.0 / 3.0; + var WIDTH = 0.4; + + var spawnPoint = Vec3.sum(Vec3.sum(MyAvatar.position, Vec3.multiply(1.0, Quat.getFront(MyAvatar.orientation))), + {x: 0, y: 0.5, z: 0}); + + var webEntityPosition = spawnPoint; + var webEntityRotation = MyAvatar.orientation; + this.webEntityID = Entities.addEntity({ + type: "Web", + sourceUrl: url, + dimensions: {x: WIDTH, y: WIDTH * ASPECT, z: 0.1}, + position: webEntityPosition, + rotation: webEntityRotation, + name: "web", + dynamic: true, + angularDamping: 0.9, + damping: 0.9, + gravity: {x: 0, y: 0, z: 0}, + shapeType: "box", + userData: JSON.stringify({ + "grabbableKey": {"grabbable": true} + }), + parentID: MyAvatar.sessionUUID, + parentJointIndex: NEGATIVE_ONE + }); + + this.state = "idle"; + + // compute the room/sensor matrix of the entity. + var invRoomMat = Mat4.inverse(MyAvatar.sensorToWorldMatrix); + var entityWorldMat = Mat4.createFromRotAndTrans(webEntityRotation, webEntityPosition); + this.entityRoomMat = Mat4.multiply(invRoomMat, entityWorldMat); + + var _this = this; + this.updateFunc = function (dt) { + _this.update(dt); + }; + Script.update.connect(this.updateFunc); +}; + +WebBuddy.prototype.destroy = function () { + Entities.deleteEntity(this.webEntityID); + Script.update.disconnect(this.updateFunc); +}; + +WebBuddy.prototype.update = function (dt) { + + var props = Entities.getEntityProperties(this.webEntityID, ["position", "rotation", "parentID", "parentJointIndex"]); + var entityWorldMat; + + if (this.state === "idle") { + + if (props.parentID !== MyAvatar.sessionUUID || props.parentJointIndex !== NEGATIVE_ONE) { + this.state = "held"; + return; + } + + // convert the sensor/room matrix of the entity into world space, using the current sensorToWorldMatrix + var roomMat = MyAvatar.sensorToWorldMatrix; + entityWorldMat = Mat4.multiply(roomMat, this.entityRoomMat); + + // slam the world space position and orientation + Entities.editEntity(this.webEntityID, { + position: Mat4.extractTranslation(entityWorldMat), + rotation: Mat4.extractRotation(entityWorldMat) + }); + } else if (this.state === "held") { + if (props.parentID === MyAvatar.sessionUUID && props.parentJointIndex === NEGATIVE_ONE) { + + // re-compute the room/sensor matrix for the avatar now that it has been released. + var invRoomMat = Mat4.inverse(MyAvatar.sensorToWorldMatrix); + entityWorldMat = Mat4.createFromRotAndTrans(props.rotation, props.position); + this.entityRoomMat = Mat4.multiply(invRoomMat, entityWorldMat); + + this.state = "idle"; + return; + } + } +}; + diff --git a/scripts/system/marketplace.js b/scripts/system/marketplace.js index 356ed8e8cf..26d46d6219 100644 --- a/scripts/system/marketplace.js +++ b/scripts/system/marketplace.js @@ -8,6 +8,9 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +/* global WebBuddy */ +Script.include("./libraries/WebBuddy.js"); + var toolIconUrl = Script.resolvePath("assets/images/tools/"); var MARKETPLACE_URL = "https://metaverse.highfidelity.com/marketplace"; @@ -22,26 +25,44 @@ var marketplaceWindow = new OverlayWebWindow({ var toolHeight = 50; var toolWidth = 50; var TOOLBAR_MARGIN_Y = 0; +var marketplaceVisible = false; +var marketplaceWebBuddy; +function shouldShowWebBuddyMarketplace() { + var rightPose = Controller.getPoseValue(Controller.Standard.RightHand); + var leftPose = Controller.getPoseValue(Controller.Standard.LeftHand); + return HMD.active && (leftPose.valid || rightPose.valid); +} function showMarketplace(marketplaceID) { - var url = MARKETPLACE_URL; - if (marketplaceID) { - url = url + "/items/" + marketplaceID; + if (shouldShowWebBuddyMarketplace()) { + marketplaceWebBuddy = new WebBuddy("https://metaverse.highfidelity.com/marketplace"); + } else { + var url = MARKETPLACE_URL; + if (marketplaceID) { + url = url + "/items/" + marketplaceID; + } + marketplaceWindow.setURL(url); + marketplaceWindow.setVisible(true); } - marketplaceWindow.setURL(url); - marketplaceWindow.setVisible(true); + marketplaceVisible = true; UserActivityLogger.openedMarketplace(); } function hideMarketplace() { - marketplaceWindow.setVisible(false); - marketplaceWindow.setURL("about:blank"); + if (marketplaceWindow.visible) { + marketplaceWindow.setVisible(false); + marketplaceWindow.setURL("about:blank"); + } else { + marketplaceWebBuddy.destroy(); + marketplaceWebBuddy = null; + } + marketplaceVisible = false; } function toggleMarketplace() { - if (marketplaceWindow.visible) { + if (marketplaceVisible) { hideMarketplace(); } else { showMarketplace();