Skip to content

Commit

Permalink
Merge pull request #6408 from mozilla/bitecs-static-media-pinnable
Browse files Browse the repository at this point in the history
[bitECS] Correctly check the Pinnable state of media in the object sidebar panel
  • Loading branch information
keianhzo authored Dec 11, 2023
2 parents 4ef68d2 + f863046 commit 472c236
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/bit-systems/object-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
Rigidbody,
Deleting,
Deletable,
MediaContentBounds
MediaLoader
} from "../bit-components";
import { anyEntityWith, findAncestorWithComponent, findAncestorWithComponents } from "../utils/bit-utils";
import { createNetworkedEntity } from "../utils/create-networked-entity";
Expand Down Expand Up @@ -241,7 +241,7 @@ function updateVisibility(world: HubsWorld, menu: EntityID, frozen: boolean) {
// need to check its state to show/hide certain buttons
// TODO At this moment all objects that have an object menu have been loaded by a media loader
// but this might not be true in the future if we allow adding object menus to arbitrary objects.
const mediaLoader = findAncestorWithComponent(world, MediaContentBounds, target);
const mediaLoader = findAncestorWithComponent(world, MediaLoader, target);
target = mediaLoader ? mediaLoader : target;

const canISpawnMove = APP.hubChannel.can("spawn_and_move_media");
Expand Down
45 changes: 27 additions & 18 deletions src/react-components/room/object-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { hasComponent } from "bitecs";
import { isPinned as getPinnedState } from "../../bit-systems/networking";
import { deleteTheDeletableAncestor } from "../../bit-systems/delete-entity-system";
import { isAEntityPinned } from "../../systems/hold-system";
import { AEntity, LocalAvatar, MediaInfo, RemoteAvatar, Static, MediaContentBounds } from "../../bit-components";
import { setPinned } from "../../utils/bit-pinning-helper";
import { AEntity, LocalAvatar, MediaInfo, RemoteAvatar, Static, MediaLoader } from "../../bit-components";
import { setPinned, canPin as canPinObject } from "../../utils/bit-pinning-helper";
import { debounce } from "lodash";

export function isMe(object) {
Expand Down Expand Up @@ -49,14 +49,7 @@ function isObjectPinned(world, eid) {
if (hasComponent(world, AEntity, eid)) {
return isAEntityPinned(APP.world, eid);
} else {
// This hook is making decisions based on components attached to the root MediaLoader entity
// and the child media entity. ie. The MediaInfo component lives in the child media but the
// networked state lives in the root. This is not great, there is a lot of places where we need to
// do component searches to find either the media root or the actual media. It's not even reasonable
// to look for a component name MediaContentBounds just to get the MediaLoader (as we remove that
// after loading the media).
// We should probably find a better way of dealing with spawned media entity hierarchies.
const mediaRootEid = findAncestorWithComponent(APP.world, MediaContentBounds, eid);
const mediaRootEid = findAncestorWithComponent(APP.world, MediaLoader, eid);
return getPinnedState(mediaRootEid);
}
}
Expand All @@ -66,7 +59,7 @@ export function usePinObject(hubChannel, scene, object) {

const pinObject = useCallback(() => {
if (shouldUseNewLoader()) {
const mediaRootEid = findAncestorWithComponent(APP.world, MediaContentBounds, object.eid);
const mediaRootEid = findAncestorWithComponent(APP.world, MediaLoader, object.eid);
setPinned(hubChannel, APP.world, mediaRootEid, true);
} else {
const el = object.el;
Expand All @@ -77,7 +70,7 @@ export function usePinObject(hubChannel, scene, object) {

const unpinObject = useCallback(() => {
if (shouldUseNewLoader()) {
const mediaRootEid = findAncestorWithComponent(APP.world, MediaContentBounds, object.eid);
const mediaRootEid = findAncestorWithComponent(APP.world, MediaLoader, object.eid);
setPinned(hubChannel, APP.world, mediaRootEid, false);
} else {
const el = object.el;
Expand Down Expand Up @@ -125,18 +118,21 @@ export function usePinObject(hubChannel, scene, object) {
};
}, [object]);

let userOwnsFile = true;
if (!shouldUseNewLoader()) {
let canBePinned = false;
if (shouldUseNewLoader()) {
const mediaRootEid = findAncestorWithComponent(APP.world, MediaLoader, object.eid);
canBePinned = canPinObject(APP.hubChannel, mediaRootEid);
} else {
const el = object.el;
if (el.components["media-loader"]) {
const { fileIsOwned, fileId } = el.components["media-loader"].data;
userOwnsFile = fileIsOwned || (fileId && getPromotionTokenForFile(fileId));
canBePinned = fileIsOwned || (fileId && getPromotionTokenForFile(fileId));
}
}

let targetEid;
if (shouldUseNewLoader()) {
targetEid = findAncestorWithComponent(APP.world, MediaContentBounds, object.eid);
targetEid = findAncestorWithComponent(APP.world, MediaLoader, object.eid);
} else {
targetEid = object.el.eid;
}
Expand All @@ -147,7 +143,7 @@ export function usePinObject(hubChannel, scene, object) {
!isPlayer(object) &&
!isStatic &&
hubChannel.can("pin_objects") &&
userOwnsFile
canBePinned
);

return { canPin, isPinned, togglePinned, pinObject, unpinObject };
Expand Down Expand Up @@ -191,12 +187,25 @@ export function useRemoveObject(hubChannel, scene, object) {

const eid = object.eid;

let canBePinned = false;
if (shouldUseNewLoader()) {
const mediaRootEid = findAncestorWithComponent(APP.world, MediaLoader, object.eid);
canBePinned = canPinObject(APP.hubChannel, mediaRootEid);
} else {
const el = object.el;
if (el.components["media-loader"]) {
const { fileIsOwned, fileId } = el.components["media-loader"].data;
canBePinned = fileIsOwned || (fileId && getPromotionTokenForFile(fileId));
}
}

const canRemoveObject = !!(
scene.is("entered") &&
!isPlayer(object) &&
!isObjectPinned(APP.world, eid) &&
!hasComponent(APP.world, Static, eid) &&
hubChannel.can("spawn_and_move_media")
hubChannel.can("spawn_and_move_media") &&
canBePinned
);

return { removeObject, canRemoveObject };
Expand Down

0 comments on commit 472c236

Please sign in to comment.