|
| 1 | +import { |
| 2 | + Entity, |
| 3 | + Layers, |
| 4 | + QueryReactor, |
| 5 | + getAuthoringCounterpart, |
| 6 | + loadEntitiesIntoAuthoring, |
| 7 | + unloadEntitiesFromAuthoring, |
| 8 | + useQuery |
| 9 | +} from '@ir-engine/ecs' |
| 10 | +import { AuthoringState } from '@ir-engine/engine/src/authoring/AuthoringState' |
| 11 | +import { GLTFComponent } from '@ir-engine/engine/src/gltf/GLTFComponent' |
| 12 | +import { SourceComponent } from '@ir-engine/engine/src/scene/components/SourceComponent' |
| 13 | +import { getState } from '@ir-engine/hyperflux' |
| 14 | +import Button from '@ir-engine/ui/src/primitives/tailwind/Button' |
| 15 | +import React, { useCallback } from 'react' |
| 16 | + |
| 17 | +export default function DynamicAuthoring() { |
| 18 | + // invoke authoring state |
| 19 | + getState(AuthoringState) |
| 20 | + |
| 21 | + return ( |
| 22 | + <> |
| 23 | + <div className="pointer-events-auto left-0 z-50 overflow-y-auto"> |
| 24 | + <QueryReactor Components={[GLTFComponent]} ChildEntityReactor={SourceLoaded} /> |
| 25 | + </div> |
| 26 | + </> |
| 27 | + ) |
| 28 | +} |
| 29 | + |
| 30 | +const SourceLoaded = (props: { entity: Entity }) => { |
| 31 | + const loaded = GLTFComponent.useSceneLoaded(props.entity) |
| 32 | + if (!loaded) return null |
| 33 | + return <SelectEditReactor entity={props.entity} /> |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Contains a button and name of the entity to select and edit it. |
| 38 | + */ |
| 39 | +const SelectEditReactor = (props: { entity: Entity }) => { |
| 40 | + const { entity } = props |
| 41 | + const isInAuthoring = !!getAuthoringCounterpart(entity) |
| 42 | + |
| 43 | + useQuery([SourceComponent], Layers.Authoring) // trigger rerender |
| 44 | + |
| 45 | + // Handle loading entity into authoring |
| 46 | + const handleLoadIntoAuthoring = useCallback(() => { |
| 47 | + try { |
| 48 | + const source = GLTFComponent.getInstanceID(entity) |
| 49 | + const entities = SourceComponent.getEntitiesBySource(source) |
| 50 | + loadEntitiesIntoAuthoring([entity, ...entities]) |
| 51 | + console.log('Loaded entities into authoring:', [entity, ...entities]) |
| 52 | + } catch (error) { |
| 53 | + console.error('Failed to load entity into authoring:', error) |
| 54 | + } |
| 55 | + }, []) |
| 56 | + |
| 57 | + // Handle unloading entity from authoring |
| 58 | + const handleUnloadFromAuthoring = useCallback(() => { |
| 59 | + try { |
| 60 | + const authoringEntity = getAuthoringCounterpart(entity) |
| 61 | + const source = GLTFComponent.getInstanceID(authoringEntity) |
| 62 | + const entities = SourceComponent.getEntitiesBySource(source, Layers.Authoring) |
| 63 | + unloadEntitiesFromAuthoring([authoringEntity, ...entities]) |
| 64 | + console.log('Unloaded entities from authoring:', [authoringEntity, ...entities]) |
| 65 | + } catch (error) { |
| 66 | + console.error('Failed to unload entity from authoring:', error) |
| 67 | + } |
| 68 | + }, []) |
| 69 | + |
| 70 | + return ( |
| 71 | + <div className="flex flex-col gap-2 rounded border border-ui-outline p-2"> |
| 72 | + <div className="text-sm font-medium">Entity ID: {entity}</div> |
| 73 | + <div className="flex gap-2"> |
| 74 | + {isInAuthoring ? ( |
| 75 | + <Button onClick={handleUnloadFromAuthoring} variant="red" size="sm"> |
| 76 | + Unload from Authoring |
| 77 | + </Button> |
| 78 | + ) : ( |
| 79 | + <Button onClick={handleLoadIntoAuthoring} variant="primary" size="sm"> |
| 80 | + Load into Authoring |
| 81 | + </Button> |
| 82 | + )} |
| 83 | + </div> |
| 84 | + </div> |
| 85 | + ) |
| 86 | +} |
0 commit comments