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): single-source the "New asset" creator registry
esengineccc4cc6db1b1
diff --git a/desktop/src/panels/ContentBrowser.tsx b/desktop/src/panels/ContentBrowser.tsxindex b85e2ad22..f4514cb4e 100644--- a/desktop/src/panels/ContentBrowser.tsx+++ b/desktop/src/panels/ContentBrowser.tsx@@ -20,20 +20,17 @@ import { findAssetUsages, type AssetUsage } from '@/project/assetUsages';import { FindUsagesDialog } from '@/components/FindUsagesDialog';import { NewTilesetDialog } from '@/components/NewTilesetDialog';import { parseAssetQuery, filterAndSortAssets, type AssetSort } from '@/project/assetFilter';+// Create-FROM-a-source-asset actions (context menu); the "New blank asset" list+// lives in the NEW_ASSET_TYPES registry.import { createTilesetFromTexture } from '@/tileset/openTileset';import { createFlipbookFromTexture } from '@/flipbook/openFlipbook';import { createAnimatedSpriteFromClip } from '@/flipbook/createAnimatedSprite';import { createTilemapFromTileset } from '@/tilemap/createTilemap';-import { BUILTIN_SHADER_TEMPLATES } from 'esengine';-import { createMaterial, createMaterialInstance, createShaderAsset } from '@/material/openMaterial';-import { createMaterialGraph } from '@/material/openMaterialGraph';-import { createStateMachine } from '@/fsm/openStateMachine';-import { createAnimatorController } from '@/animator/openAnimatorController';+import { createMaterialInstance } from '@/material/openMaterial';+import { NEW_ASSET_TYPES, type CreateAsset } from '@/project/newAssetTypes';import { onAssetReveal } from '@/project/assetReveal';-import { createBehaviorTree } from '@/bt/openBehaviorTree';-import { createAnimationClip } from '@/timeline/openClip';import { fsRefresh } from '@/project/fsWatch';-import { t } from '@/i18n';+import { t, type MsgKey } from '@/i18n';import type { DirEntry } from '@/project/format';import type { AssetType } from '@/types';@@ -712,38 +709,27 @@ export function ContentBrowser() {}};- const newScene = async () => {+ // Run a NEW_ASSET_TYPES creator. A returned path is a blank-file asset with no+ // editor of its own → reveal it + drop into rename (like New Folder); void means+ // the creator opened its own editor and refreshed/toasted itself.+ const runNew = async (create: CreateAsset, errorKey?: MsgKey) => {try {- const path = await ProjectStore.createSceneFile(cwd);- refreshFs();- selectAsset(path);- setQuery(""); setFilters(new Set()); startRename(path); // drop into rename, like New Folder- } catch (e) {- Toasts.push(t('cb.newSceneFailed', { error: errMsg(e) }), 'error');- }- };-- const newInputMap = async () => {- try {- const path = await ProjectStore.createInputMapFile(cwd);- refreshFs();- selectAsset(path); // unified inspector opens the input-map editor- setQuery(""); setFilters(new Set()); startRename(path);+ const path = await create(cwd);+ if (typeof path === 'string') {+ refreshFs();+ selectAsset(path);+ setQuery(""); setFilters(new Set()); startRename(path);+ }} catch (e) {- Toasts.push(t('cb.newInputMapFailed', { error: errMsg(e) }), 'error');+ if (errorKey) Toasts.push(t(errorKey, { error: errMsg(e) }), 'error');}};- const newLocaleTable = async () => {- try {- const path = await ProjectStore.createLocaleTableFile(cwd);- refreshFs();- selectAsset(path);- setQuery(""); setFilters(new Set()); startRename(path);- } catch (e) {- Toasts.push(t('cb.newLocaleTableFailed', { error: errMsg(e) }), 'error');- }- };+ const newAssetItems: MenuItem[] = NEW_ASSET_TYPES.map((a) =>+ a.templates+ ? { label: t(a.labelKey), children: a.templates().map((tpl) => ({ label: tpl.label, onClick: () => void runNew(tpl.create) })) }+ : { label: t(a.labelKey), onClick: () => void runNew(a.create!, a.errorKey) },+ );const showInExplorer = async (path: string) => {try {@@ -987,28 +973,7 @@ export function ContentBrowser() {{ label: t('cb.menuImport'), icon: <Import size={14} />, onClick: () => void importAssets() },{ label: t('cb.menuNewFolder'), icon: <FolderPlus size={14} />, onClick: () => void newFolder() },{ sep: true },- { label: t('cb.menuNewScene'), onClick: () => void newScene() },- { label: t('cb.menuNewAnimation'), onClick: () => void createAnimationClip(cwd) },- { label: t('cb.menuNewInputMap'), onClick: () => void newInputMap() },- { label: t('cb.menuNewLocaleTable'), onClick: () => void newLocaleTable() },- {- label: t('cb.menuNewMaterial'),- children: BUILTIN_SHADER_TEMPLATES.map((tpl) => ({- label: tpl.label,- onClick: () => void createMaterial(cwd, tpl.id),- })),- },- { label: t('cb.menuNewMaterialGraph'), onClick: () => void createMaterialGraph(cwd) },- {- label: t('cb.menuNewShader'),- children: BUILTIN_SHADER_TEMPLATES.map((tpl) => ({- label: tpl.label,- onClick: () => void createShaderAsset(cwd, tpl.id),- })),- },- { label: t('cb.menuNewStateMachine'), onClick: () => void createStateMachine(cwd) },- { label: t('cb.menuNewAnimatorController'), onClick: () => void createAnimatorController(cwd) },- { label: t('cb.menuNewBehaviorTree'), onClick: () => void createBehaviorTree(cwd) },+ ...newAssetItems,{ sep: true },{ label: t('cb.menuShowInExplorer'), onClick: () => void showInExplorer(cwd) },];diff --git a/desktop/src/project/newAssetTypes.ts b/desktop/src/project/newAssetTypes.tsnew file mode 100644index 000000000..08f88cbb0--- /dev/null+++ b/desktop/src/project/newAssetTypes.ts@@ -0,0 +1,54 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file newAssetTypes.ts+ * @brief The single list of "New …" asset creators behind the Content Browser's+ * create menu. One entry per creatable type carries its menu label and its+ * `create(dir)` — so adding a type is one entry here, not a menu row + an+ * import + a bespoke wrapper scattered across ContentBrowser.+ *+ * Two creator shapes, distinguished by return value:+ * - blank-file assets (scene / input map / locale table) return the new path so+ * the Content Browser reveals + drops it into rename; they have no editor of+ * their own, so a failure toast key is supplied here.+ * - editor-backed assets (graphs / material / animation) open their own editor+ * and refresh + toast themselves, so they return void and carry no error key.+ */+import type { MsgKey } from '@/i18n';+import { BUILTIN_SHADER_TEMPLATES } from 'esengine';+import { ProjectStore } from './ProjectStore';+import { createAnimationClip } from '@/timeline/openClip';+import { createMaterial, createShaderAsset } from '@/material/openMaterial';+import { createMaterialGraph } from '@/material/openMaterialGraph';+import { createStateMachine } from '@/fsm/openStateMachine';+import { createAnimatorController } from '@/animator/openAnimatorController';+import { createBehaviorTree } from '@/bt/openBehaviorTree';++/** Create an asset in `dir`; a returned path means "reveal + rename me" (see file). */+export type CreateAsset = (dir: string) => Promise<string | void> | string | void;++export interface NewAssetEntry {+ labelKey: MsgKey;+ /** Direct creator (mutually exclusive with `templates`). */+ create?: CreateAsset;+ /** A submenu — one creator per built-in template (the shader/material picker). */+ templates?: () => { label: string; create: CreateAsset }[];+ /** Failure toast key; only blank-file creators need it (editors toast themselves). */+ errorKey?: MsgKey;+}++const shaderTemplateMenu = (make: (dir: string, templateId: string) => Promise<void>) => () =>+ BUILTIN_SHADER_TEMPLATES.map((tpl) => ({ label: tpl.label, create: (dir: string) => make(dir, tpl.id) }));++export const NEW_ASSET_TYPES: NewAssetEntry[] = [+ { labelKey: 'cb.menuNewScene', create: (dir) => ProjectStore.createSceneFile(dir), errorKey: 'cb.newSceneFailed' },+ { labelKey: 'cb.menuNewAnimation', create: createAnimationClip },+ { labelKey: 'cb.menuNewInputMap', create: (dir) => ProjectStore.createInputMapFile(dir), errorKey: 'cb.newInputMapFailed' },+ { labelKey: 'cb.menuNewLocaleTable', create: (dir) => ProjectStore.createLocaleTableFile(dir), errorKey: 'cb.newLocaleTableFailed' },+ { labelKey: 'cb.menuNewMaterial', templates: shaderTemplateMenu(createMaterial) },+ { labelKey: 'cb.menuNewMaterialGraph', create: createMaterialGraph },+ { labelKey: 'cb.menuNewShader', templates: shaderTemplateMenu(createShaderAsset) },+ { labelKey: 'cb.menuNewStateMachine', create: createStateMachine },+ { labelKey: 'cb.menuNewAnimatorController', create: createAnimatorController },+ { labelKey: 'cb.menuNewBehaviorTree', create: createBehaviorTree },+];diff --git a/desktop/tests/new-asset-types.test.ts b/desktop/tests/new-asset-types.test.tsnew file mode 100644index 000000000..9a1d8b754--- /dev/null+++ b/desktop/tests/new-asset-types.test.ts@@ -0,0 +1,51 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+import { describe, it, expect } from 'vitest';+import { NEW_ASSET_TYPES } from '../src/project/newAssetTypes';++describe('NEW_ASSET_TYPES', () => {+ it('every entry is either a direct creator or a template submenu, never both/neither', () => {+ for (const a of NEW_ASSET_TYPES) {+ const direct = typeof a.create === 'function';+ const submenu = typeof a.templates === 'function';+ expect(direct !== submenu, `${a.labelKey} must have exactly one of create/templates`).toBe(true);+ }+ });++ it('only blank-file (direct) creators carry an error toast key', () => {+ // Template submenus and editor-backed creators toast themselves.+ for (const a of NEW_ASSET_TYPES) {+ if (a.errorKey) expect(typeof a.create).toBe('function');+ }+ });++ it('label keys are unique (no accidental duplicate menu row)', () => {+ const keys = NEW_ASSET_TYPES.map((a) => a.labelKey);+ expect(new Set(keys).size).toBe(keys.length);+ });++ it('template submenus resolve to at least one creator each', () => {+ for (const a of NEW_ASSET_TYPES) {+ if (a.templates) {+ const items = a.templates();+ expect(items.length).toBeGreaterThan(0);+ for (const it of items) expect(typeof it.create).toBe('function');+ }+ }+ });++ it('covers the expected creatable types', () => {+ expect(NEW_ASSET_TYPES.map((a) => a.labelKey)).toEqual([+ 'cb.menuNewScene',+ 'cb.menuNewAnimation',+ 'cb.menuNewInputMap',+ 'cb.menuNewLocaleTable',+ 'cb.menuNewMaterial',+ 'cb.menuNewMaterialGraph',+ 'cb.menuNewShader',+ 'cb.menuNewStateMachine',+ 'cb.menuNewAnimatorController',+ 'cb.menuNewBehaviorTree',+ ]);+ });+});