Skip to content

Commit

Permalink
Merge pull request #5921 from mozilla/newloader-anoyances
Browse files Browse the repository at this point in the history
newLoader anoyances
  • Loading branch information
johnshaughnessy authored Jan 31, 2023
2 parents b83cd5d + fcb296b commit 267e53b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
15 changes: 13 additions & 2 deletions src/load-media-on-paste-or-drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type UploadResponse = {
origin: string;
};

function spawnFromUrl(text: string) {
export function spawnFromUrl(text: string) {
if (!text) {
return;
}
Expand All @@ -36,7 +36,7 @@ function spawnFromUrl(text: string) {
obj.lookAt(avatarPov.getWorldPosition(new Vector3()));
}

async function spawnFromFileList(files: FileList) {
export async function spawnFromFileList(files: FileList) {
for (const file of files) {
const desiredContentType = file.type || guessContentType(file.name);
const params = await upload(file, desiredContentType)
Expand Down Expand Up @@ -94,10 +94,21 @@ async function onPaste(e: ClipboardEvent) {
spawnFromUrl(text);
}

let lastDebugScene: string;
function onDrop(e: DragEvent) {
if (!(AFRAME as any).scenes[0].is("entered")) {
return;
}

if (qsTruthy("debugLocalScene")) {
URL.revokeObjectURL(lastDebugScene);
if (!e.dataTransfer?.files.length) return;
const url = URL.createObjectURL(e.dataTransfer.files[0]);
APP.hubChannel!.updateScene(url);
lastDebugScene = url;
return;
}

const files = e.dataTransfer?.files;
if (files && files.length) {
e.preventDefault();
Expand Down
28 changes: 24 additions & 4 deletions src/scene-entry-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import { addComponent, removeEntity } from "bitecs";
import { MyCameraTool } from "./bit-components";
import { anyEntityWith } from "./utils/bit-utils";
import { moveToSpawnPoint } from "./bit-systems/waypoint";
import { spawnFromFileList, spawnFromUrl } from "./load-media-on-paste-or-drop";

const useNewLoader = qsTruthy("newLoader");

export default class SceneEntryManager {
constructor(hubChannel, authChannel, history) {
Expand Down Expand Up @@ -76,7 +79,7 @@ export default class SceneEntryManager {
await exit2DInterstitialAndEnterVR(true);
}

if (qsTruthy("newLoader")) {
if (useNewLoader) {
moveToSpawnPoint(APP.world, this.scene.systems["hubs-systems"].characterController);
} else {
const waypointSystem = this.scene.systems["hubs-systems"].waypointSystem;
Expand Down Expand Up @@ -215,9 +218,26 @@ export default class SceneEntryManager {
};

_setupMedia = () => {
const offset = { x: 0, y: 0, z: -1.5 };
const spawnMediaInfrontOfPlayer = (src, contentOrigin) => {
console.warn(
"Spawning newLoader object using `spawnMediaInFrontOfPlayer`. This codepath should likely be made more direct.",
src,
contentOrigin
);
if (useNewLoader) {
if (typeof src === "string") {
spawnFromUrl(src);
} else {
spawnFromFileList([src]);
}
} else {
spawnMediaInfrontOfPlayerAndReturn(src, contentOrigin).eid;
}
};
// HACK we only care about the return value in 1 spot, don't want to deal with that in the newLoader path
const spawnMediaInfrontOfPlayerAndReturn = (src, contentOrigin) => {
if (!this.hubChannel.can("spawn_and_move_media")) return;
const offset = { x: 0, y: 0, z: -1.5 };
const { entity, orientation } = addMedia(
src,
"#interactable-media",
Expand Down Expand Up @@ -270,7 +290,7 @@ export default class SceneEntryManager {

this.scene.addEventListener("action_vr_notice_closed", () => forceExitFrom2DInterstitial());

if (!qsTruthy("newLoader")) {
if (!useNewLoader) {
document.addEventListener("paste", e => {
if (
(e.target.matches("input, textarea") || e.target.contentEditable === "true") &&
Expand Down Expand Up @@ -340,7 +360,7 @@ export default class SceneEntryManager {
if (target === "avatar") {
this.avatarRig.setAttribute("player-info", { isSharingAvatarCamera: true });
} else {
currentVideoShareEntity = spawnMediaInfrontOfPlayer(this.mediaDevicesManager.mediaStream, undefined);
currentVideoShareEntity = spawnMediaInfrontOfPlayerAndReturn(this.mediaDevicesManager.mediaStream, undefined);
// Wire up custom removal event which will stop the stream.
currentVideoShareEntity.setAttribute(
"emit-scene-event-on-remove",
Expand Down
15 changes: 14 additions & 1 deletion src/utils/load-image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,22 @@ import { HubsWorld } from "../app";
import { Texture } from "three";
import { AlphaMode } from "./create-image-mesh";

// TODO AlphaMode is written in JS and should be a ts enum
type AlphaModeType = typeof AlphaMode.Opaque | typeof AlphaMode.Mask | typeof AlphaMode.Blend;
export function* loadImage(world: HubsWorld, url: string, contentType: string) {
const { texture, ratio, cacheKey }: { texture: Texture; ratio: number; cacheKey: string } =
yield loadTextureCancellable(url, 1, contentType);

// TODO it would be nice if we could be less agressive with transparency here.
// Doing so requires inspecting the raw file data upstream of here and passing
// that info through somehow which feels tricky.
let alphaMode: AlphaModeType = AlphaMode.Blend;
if (contentType === "image/gif") {
alphaMode = AlphaMode.Mask;
} else if (contentType === "image/jpeg") {
alphaMode = AlphaMode.Opaque;
}

return renderAsEntity(
world,
<entity
Expand All @@ -18,7 +31,7 @@ export function* loadImage(world: HubsWorld, url: string, contentType: string) {
texture,
ratio,
projection: ProjectionMode.FLAT,
alphaMode: AlphaMode.Opaque,
alphaMode,
cacheKey
}}
/>
Expand Down

0 comments on commit 267e53b

Please sign in to comment.