Add audio asset handling and import command
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { compareEntityInstances, getEntityKindLabel, getEntityInstances, type EntityInstance } from "./entity-instances";
|
||||
import type { ProjectAssetRecord } from "../assets/project-assets";
|
||||
|
||||
function getSortedEntitiesByKind(entities: Record<string, EntityInstance>, kind: EntityInstance["kind"]): EntityInstance[] {
|
||||
return Object.values(entities)
|
||||
@@ -6,27 +7,61 @@ function getSortedEntitiesByKind(entities: Record<string, EntityInstance>, kind:
|
||||
.sort(compareEntityInstances);
|
||||
}
|
||||
|
||||
export function getEntityDisplayLabel(entity: EntityInstance, entities: Record<string, EntityInstance>): string {
|
||||
function getSoundEmitterLabelSuffix(entity: EntityInstance & { kind: "soundEmitter" }, assets?: Record<string, ProjectAssetRecord>): string {
|
||||
if (entity.audioAssetId === null) {
|
||||
return "No Audio Asset";
|
||||
}
|
||||
|
||||
const asset = assets?.[entity.audioAssetId];
|
||||
|
||||
if (asset === undefined) {
|
||||
return `Missing Audio Asset (${entity.audioAssetId})`;
|
||||
}
|
||||
|
||||
if (asset.kind !== "audio") {
|
||||
return `Invalid Audio Asset (${asset.sourceName})`;
|
||||
}
|
||||
|
||||
return asset.sourceName;
|
||||
}
|
||||
|
||||
export function getEntityDisplayLabel(
|
||||
entity: EntityInstance,
|
||||
entities: Record<string, EntityInstance>,
|
||||
assets?: Record<string, ProjectAssetRecord>
|
||||
): string {
|
||||
const typedEntities = getSortedEntitiesByKind(entities, entity.kind);
|
||||
const entityIndex = typedEntities.findIndex((candidate) => candidate.id === entity.id);
|
||||
const baseLabel = getEntityKindLabel(entity.kind);
|
||||
const numberedLabel = entityIndex <= 0 ? baseLabel : `${baseLabel} ${entityIndex + 1}`;
|
||||
|
||||
return entityIndex <= 0 ? baseLabel : `${baseLabel} ${entityIndex + 1}`;
|
||||
if (entity.kind !== "soundEmitter" || assets === undefined) {
|
||||
return numberedLabel;
|
||||
}
|
||||
|
||||
return `${numberedLabel} · ${getSoundEmitterLabelSuffix(entity, assets)}`;
|
||||
}
|
||||
|
||||
export function getEntityDisplayLabelById(entityId: string, entities: Record<string, EntityInstance>): string {
|
||||
export function getEntityDisplayLabelById(
|
||||
entityId: string,
|
||||
entities: Record<string, EntityInstance>,
|
||||
assets?: Record<string, ProjectAssetRecord>
|
||||
): string {
|
||||
const entity = entities[entityId];
|
||||
|
||||
if (entity === undefined) {
|
||||
return "Entity";
|
||||
}
|
||||
|
||||
return getEntityDisplayLabel(entity, entities);
|
||||
return getEntityDisplayLabel(entity, entities, assets);
|
||||
}
|
||||
|
||||
export function getSortedEntityDisplayLabels(entities: Record<string, EntityInstance>): Array<{ entity: EntityInstance; label: string }> {
|
||||
export function getSortedEntityDisplayLabels(
|
||||
entities: Record<string, EntityInstance>,
|
||||
assets?: Record<string, ProjectAssetRecord>
|
||||
): Array<{ entity: EntityInstance; label: string }> {
|
||||
return getEntityInstances(entities).map((entity) => ({
|
||||
entity,
|
||||
label: getEntityDisplayLabel(entity, entities)
|
||||
label: getEntityDisplayLabel(entity, entities, assets)
|
||||
}));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user