A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
refactor(editor): one declarative registry for asset types
esengine886d7f330ca9
diff --git a/desktop/src/components/icons.tsx b/desktop/src/components/icons.tsxindex c25252b08..08e18a92f 100644--- a/desktop/src/components/icons.tsx+++ b/desktop/src/components/icons.tsx@@ -10,15 +10,7 @@ import {Volume2,Lightbulb,CircleDot,- Folder,- Film,- Clapperboard,- FileImage,- Music,Component,- Blend,- FileCode2,- File,Type,Grid3x3,Sparkles,@@ -28,6 +20,7 @@ import {type LucideIcon,} from 'lucide-react';import type { NodeKind, AssetType } from '@/types';+import { ASSET_TYPES } from '@/project/assetTypes';// Scene-node kind → glyph. Keeps the outliner legible at a glance.const NODE_ICON: Record<NodeKind, LucideIcon> = {@@ -47,31 +40,13 @@ export function NodeIcon({ kind, size = 14 }: { kind: NodeKind; size?: number })return <Glyph size={size} strokeWidth={1.75} />;}-// Asset type → glyph + muted type tint. Desaturated (vs candy colors) so the-// content browser stays scannable by type but reads as a professional tool.-const ASSET_ICON: Record<AssetType, { icon: LucideIcon; tint: string }> = {- folder: { icon: Folder, tint: 'var(--star)' },- scene: { icon: Film, tint: '#c98a93' },- sprite: { icon: Image, tint: '#7fa6c4' },- texture: { icon: FileImage, tint: '#7fa6c4' },- spine: { icon: PersonStanding, tint: '#9b8fc0' },- audio: { icon: Music, tint: '#7faf9c' },- prefab: { icon: Component, tint: '#c2a274' },- material: { icon: Blend, tint: '#c0917a' },- script: { icon: FileCode2, tint: '#93a3bf' },- animation: { icon: Clapperboard, tint: '#9bb39a' },- tileset: { icon: Grid3x3, tint: '#9b8fc0' },- tilemap: { icon: Grid3x3, tint: '#7fa6c4' },- file: { icon: File, tint: 'var(--text-dim)' },-};-export function AssetIcon({ type, size = 22 }: { type: AssetType; size?: number }) {- const { icon: Glyph, tint } = ASSET_ICON[type];+ const { icon: Glyph, tint } = ASSET_TYPES[type];return <Glyph size={size} strokeWidth={1.5} color={tint} />;}export function assetTint(type: AssetType): string {- return ASSET_ICON[type].tint;+ return ASSET_TYPES[type].tint;}// Component (by registry name) → glyph, for the Add-Component picker. Knowndiff --git a/desktop/src/panels/ContentBrowser.tsx b/desktop/src/panels/ContentBrowser.tsxindex ed7a88b53..98e440596 100644--- a/desktop/src/panels/ContentBrowser.tsx+++ b/desktop/src/panels/ContentBrowser.tsx@@ -6,12 +6,11 @@ import { ChevronRight, Search, LayoutGrid, List, Import, FolderOpen, FolderPlus,import { AssetIcon, assetTint } from '@/components/icons';import { ContextMenu, type MenuItem } from '@/components/Menu';import { ProjectStore } from '@/project/ProjectStore';-import { EditorHistory } from '@/engine/EditorHistory';import { Toasts } from '@/store/Toasts';import { useSelection } from '@/store/selectionStore';import { IMAGE_RE, assetTypeOf as assetType, TYPE_CODE } from '@/project/assetMeta';-import { openAnimationClip } from '@/timeline/openClip';-import { openTileset, createTilesetFromTexture } from '@/tileset/openTileset';+import { ASSET_OPEN } from '@/project/assetOpen';+import { createTilesetFromTexture } from '@/tileset/openTileset';import { createTilemapFromTileset } from '@/tilemap/createTilemap';import { fsRefresh } from '@/project/fsWatch';import type { DirEntry } from '@/project/format';@@ -294,21 +293,14 @@ export function ContentBrowser() {return [...list].sort((a, b) => (a.isDir === b.isDir ? a.name.localeCompare(b.name) : a.isDir ? -1 : 1));}, [entries, q, filters]);- // Double-click: enter folders; open scenes as the editor document (guarding- // unsaved edits — history is cleared on open, so canUndo ≈ "edited this session").+ // Double-click: enter folders; otherwise dispatch through the per-type open+ // table (scene/clip/tileset editors).const onOpen = (path: string, isDir: boolean, name: string) => {if (isDir) {go(path);return;}- if (assetType(name) === 'scene') {- if (EditorHistory.canUndo() && !window.confirm(`Open ${name}? Unsaved changes will be lost.`)) return;- void ProjectStore.openScene(path);- } else if (assetType(name) === 'animation') {- void openAnimationClip(path);- } else if (assetType(name) === 'tileset') {- void openTileset(path);- }+ ASSET_OPEN[assetType(name)]?.(path, name);};// After any fs mutation: re-read open directories + re-scan the asset registrydiff --git a/desktop/src/project/assetMeta.ts b/desktop/src/project/assetMeta.tsindex 9a458bc8b..54e642bd6 100644--- a/desktop/src/project/assetMeta.ts+++ b/desktop/src/project/assetMeta.ts@@ -2,46 +2,20 @@// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team/*** @file assetMeta.ts- * @brief Asset-type derivation shared by the Content Browser and the unified- * Details inspector (so selecting an asset resolves the same type/badge- * everywhere). Type is inferred from the file extension.+ * @brief Re-exports the asset-type helpers used by the Content Browser and the+ * unified Details inspector, derived from the single `assetTypes` registry+ * so selecting an asset resolves the same type/badge everywhere.*/import type { AssetType } from '@/types';+import { ASSET_TYPES, assetTypeOf } from './assetTypes';-export const IMAGE_RE = /\.(png|jpe?g|webp|gif)$/i;+export { assetTypeOf };-/** Asset type from a file name's extension. */-export function assetTypeOf(name: string): AssetType {- const ext = name.split('.').pop()?.toLowerCase() ?? '';- if (IMAGE_RE.test(name)) return ext === 'png' || ext === 'webp' ? 'texture' : 'sprite';- if (ext === 'esscene') return 'scene';- if (ext === 'ogg' || ext === 'mp3' || ext === 'wav') return 'audio';- if (ext === 'ts' || ext === 'js') return 'script';- if (ext === 'atlas' || ext === 'skel') return 'spine';- if (ext === 'esprefab') return 'prefab';- if (ext === 'esmat') return 'material';- // Unified animation clip (.esanim) + legacy multi-track timeline (.estimeline).- if (ext === 'esanim' || ext === 'estimeline') return 'animation';- if (ext === 'estileset') return 'tileset';- if (ext === 'estilemap') return 'tilemap';- return 'file';-}+export const IMAGE_RE = /\.(png|jpe?g|webp|gif)$/i;/** Short uppercase code shown in a tile's corner badge / detail tag, per type. */-export const TYPE_CODE: Record<AssetType, string> = {- folder: '',- file: '',- scene: 'SCN',- prefab: 'PFB',- texture: 'TEX',- sprite: 'IMG',- material: 'MAT',- spine: 'SPN',- audio: 'AUD',- script: 'TS',- animation: 'ANM',- tileset: 'TST',- tilemap: 'TMP',-};+export const TYPE_CODE = Object.fromEntries(+ (Object.entries(ASSET_TYPES) as [AssetType, { badge: string }][]).map(([type, def]) => [type, def.badge]),+) as Record<AssetType, string>;export const baseName = (p: string) => (p.includes('/') ? p.slice(p.lastIndexOf('/') + 1) : p);diff --git a/desktop/src/project/assetOpen.ts b/desktop/src/project/assetOpen.tsnew file mode 100644index 000000000..de14d862c--- /dev/null+++ b/desktop/src/project/assetOpen.ts@@ -0,0 +1,25 @@+// SPDX-License-Identifier: LicenseRef-PolyForm-Noncommercial-1.0.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file assetOpen.ts+ * @brief The double-click open action per asset type — the dispatch behind the+ * Content Browser. Kept separate from the pure `assetTypes` registry+ * because the actions pull in heavy editor modules (scene/clip/tileset+ * openers); folding them into the registry would create import cycles.+ */+import type { AssetType } from '@/types';+import { ProjectStore } from './ProjectStore';+import { EditorHistory } from '@/engine/EditorHistory';+import { openAnimationClip } from '@/timeline/openClip';+import { openTileset } from '@/tileset/openTileset';++/** Open action per asset type; types absent here aren't double-click-openable. */+export const ASSET_OPEN: Partial<Record<AssetType, (path: string, name: string) => void>> = {+ scene: (path, name) => {+ // History is cleared on open, so canUndo ≈ "edited this session" — warn first.+ if (EditorHistory.canUndo() && !window.confirm(`Open ${name}? Unsaved changes will be lost.`)) return;+ void ProjectStore.openScene(path);+ },+ animation: (path) => void openAnimationClip(path),+ tileset: (path) => void openTileset(path),+};diff --git a/desktop/src/project/assetTypes.ts b/desktop/src/project/assetTypes.tsnew file mode 100644index 000000000..319f8a5f7--- /dev/null+++ b/desktop/src/project/assetTypes.ts@@ -0,0 +1,59 @@+// SPDX-License-Identifier: LicenseRef-PolyForm-Noncommercial-1.0.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file assetTypes.ts+ * @brief The single declarative registry of editor asset types. One entry per+ * type carries its file extensions, content-browser badge, and icon/tint;+ * `assetTypeOf` (extension lookup), the `TYPE_CODE` badges, and the+ * `AssetIcon`/`assetTint` glyphs all derive from it. Adding an asset type+ * is this table + the `AssetType` union in types.ts (the `Record` keeps+ * them in sync — a missing entry is a compile error). Double-click open+ * actions live in assetOpen.ts, co-located with their editors to avoid+ * import cycles.+ */+import {+ Folder, Film, Image, FileImage, PersonStanding, Music,+ Component, Blend, FileCode2, Clapperboard, Grid3x3, File,+ type LucideIcon,+} from 'lucide-react';+import type { AssetType } from '@/types';++export interface AssetTypeDef {+ /** Extensions (lower-case, no dot) that resolve to this type. Omitted for the+ * virtual `folder`/`file` types, which aren't extension-derived. */+ extensions?: readonly string[];+ /** Short uppercase code shown in a tile's corner badge ('' = no badge). */+ badge: string;+ icon: LucideIcon;+ tint: string;+}++// Desaturated tints (vs candy colors) so the content browser stays scannable by+// type but reads as a professional tool.+export const ASSET_TYPES: Record<AssetType, AssetTypeDef> = {+ folder: { badge: '', icon: Folder, tint: 'var(--star)' },+ scene: { extensions: ['esscene'], badge: 'SCN', icon: Film, tint: '#c98a93' },+ texture: { extensions: ['png', 'webp'], badge: 'TEX', icon: FileImage, tint: '#7fa6c4' },+ sprite: { extensions: ['jpg', 'jpeg', 'gif'], badge: 'IMG', icon: Image, tint: '#7fa6c4' },+ spine: { extensions: ['atlas', 'skel'], badge: 'SPN', icon: PersonStanding, tint: '#9b8fc0' },+ audio: { extensions: ['ogg', 'mp3', 'wav'], badge: 'AUD', icon: Music, tint: '#7faf9c' },+ prefab: { extensions: ['esprefab'], badge: 'PFB', icon: Component, tint: '#c2a274' },+ material: { extensions: ['esmat'], badge: 'MAT', icon: Blend, tint: '#c0917a' },+ script: { extensions: ['ts', 'js'], badge: 'TS', icon: FileCode2, tint: '#93a3bf' },+ // Unified animation clip (.esanim) + legacy multi-track timeline (.estimeline).+ animation: { extensions: ['esanim', 'estimeline'], badge: 'ANM', icon: Clapperboard, tint: '#9bb39a' },+ tileset: { extensions: ['estileset'], badge: 'TST', icon: Grid3x3, tint: '#9b8fc0' },+ tilemap: { extensions: ['estilemap'], badge: 'TMP', icon: Grid3x3, tint: '#7fa6c4' },+ file: { badge: '', icon: File, tint: 'var(--text-dim)' },+};++const byExt = new Map<string, AssetType>();+for (const [type, def] of Object.entries(ASSET_TYPES) as [AssetType, AssetTypeDef][]) {+ for (const ext of def.extensions ?? []) byExt.set(ext, type);+}++/** Asset type from a file name's extension; unknown extensions fall back to `file`. */+export function assetTypeOf(name: string): AssetType {+ const ext = name.split('.').pop()?.toLowerCase() ?? '';+ return byExt.get(ext) ?? 'file';+}diff --git a/desktop/tests/asset-types.test.ts b/desktop/tests/asset-types.test.tsnew file mode 100644index 000000000..641550cb3--- /dev/null+++ b/desktop/tests/asset-types.test.ts@@ -0,0 +1,43 @@+// SPDX-License-Identifier: LicenseRef-PolyForm-Noncommercial-1.0.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+import { describe, it, expect } from 'vitest';+import { assetTypeOf, ASSET_TYPES } from '@/project/assetTypes';+import { TYPE_CODE } from '@/project/assetMeta';++describe('asset type registry', () => {+ it('maps file extensions to the editor asset type', () => {+ expect(assetTypeOf('a.esscene')).toBe('scene');+ expect(assetTypeOf('a.png')).toBe('texture');+ expect(assetTypeOf('a.webp')).toBe('texture');+ expect(assetTypeOf('a.jpg')).toBe('sprite');+ expect(assetTypeOf('a.gif')).toBe('sprite');+ expect(assetTypeOf('a.ogg')).toBe('audio');+ expect(assetTypeOf('a.ts')).toBe('script');+ expect(assetTypeOf('a.atlas')).toBe('spine');+ expect(assetTypeOf('a.skel')).toBe('spine');+ expect(assetTypeOf('a.esprefab')).toBe('prefab');+ expect(assetTypeOf('a.esmat')).toBe('material');+ expect(assetTypeOf('a.esanim')).toBe('animation');+ expect(assetTypeOf('a.estimeline')).toBe('animation');+ expect(assetTypeOf('a.estileset')).toBe('tileset');+ expect(assetTypeOf('a.estilemap')).toBe('tilemap');+ expect(assetTypeOf('a.bogus')).toBe('file');+ expect(assetTypeOf('noextension')).toBe('file');+ });++ it('derives TYPE_CODE badges from the registry', () => {+ expect(TYPE_CODE.scene).toBe('SCN');+ expect(TYPE_CODE.tileset).toBe('TST');+ expect(TYPE_CODE.folder).toBe('');+ for (const [type, def] of Object.entries(ASSET_TYPES)) {+ expect(TYPE_CODE[type as keyof typeof TYPE_CODE]).toBe(def.badge);+ }+ });++ it('every type carries an icon and tint', () => {+ for (const def of Object.values(ASSET_TYPES)) {+ expect(def.icon).toBeTruthy();+ expect(def.tint).toBeTruthy();+ }+ });+});