Skip to content

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.

feat(editor): auto-generate anchor-component create sources (E6)

esengine4b5536db2053
diff --git a/desktop/src/components/CreatePopover.tsx b/desktop/src/components/CreatePopover.tsx
index e2a2eb64a..78811fb52 100644
--- a/desktop/src/components/CreatePopover.tsx
+++ b/desktop/src/components/CreatePopover.tsx
@@ -12,23 +12,28 @@
*/
import { useEffect, useMemo, useRef, useState } from 'react';
import { createPortal } from 'react-dom';
-import { ENTITY_SOURCES, userComponentSources, matchSources, type EntitySource } from '@/engine/entitySources';
+import { ENTITY_SOURCES, userComponentSources, matchSources, CREATE_CATEGORY_ORDER, type EntitySource } from '@/engine/entitySources';
import { ProjectStore } from '@/project/ProjectStore';
import { SearchField } from '@/components/SearchField';
import { useDialogFocus } from '@/components/dialogFocus';
-/** Group consecutive sources by category (the registry is already category-ordered). */
+/** Group sources by category, ordered by CREATE_CATEGORY_ORDER — sources arrive in
+ * mixed category order now that anchors are auto-generated and dynamic sources
+ * (user components, prefabs) are appended. Unknown categories trail at the end. */
function groupByCategory(items: EntitySource[]) {
- const groups: { category: string; items: EntitySource[] }[] = [];
+ const byCat = new Map<string, EntitySource[]>();
for (const it of items) {
- let g = groups[groups.length - 1];
- if (!g || g.category !== it.category) {
- g = { category: it.category, items: [] };
- groups.push(g);
- }
- g.items.push(it);
+ const arr = byCat.get(it.category);
+ if (arr) arr.push(it);
+ else byCat.set(it.category, [it]);
}
- return groups;
+ const rank = (c: string) => {
+ const i = CREATE_CATEGORY_ORDER.indexOf(c);
+ return i < 0 ? CREATE_CATEGORY_ORDER.length : i;
+ };
+ return [...byCat.entries()]
+ .sort((a, b) => rank(a[0]) - rank(b[0]))
+ .map(([category, items]) => ({ category, items }));
}
export function CreatePopover({
diff --git a/desktop/src/engine/entitySources.ts b/desktop/src/engine/entitySources.ts
index 7f623e58f..c46e583f3 100644
--- a/desktop/src/engine/entitySources.ts
+++ b/desktop/src/engine/entitySources.ts
@@ -11,10 +11,10 @@
* UI. (REARCH_ENTITY_CREATION E2.)
*/
import type { LucideIcon } from 'lucide-react';
-import { CircleDot, Image, Camera, Sparkles, Lightbulb, LayoutPanelTop, ToggleLeft, SlidersHorizontal, List, ChevronDown, SquareMousePointer, RectangleHorizontal, Box } from 'lucide-react';
+import { CircleDot, LayoutPanelTop, ToggleLeft, SlidersHorizontal, List, ChevronDown, SquareMousePointer, RectangleHorizontal, Box } from 'lucide-react';
import { BUILTIN_UI_PREFABS, BUILTIN_UI_WIDGET_NAMES, PREFAB_FORMAT_VERSION, getUserComponents, type PrefabData } from 'esengine';
import type { EntityId } from '@/types';
-import { componentByName, componentDefaults, prettyLabel } from './schema';
+import { componentByName, componentDefaults, prettyLabel, componentCategory } from './schema';
import { componentGlyph } from '@/components/icons';
import { SceneCommands } from './SceneCommands';
@@ -44,8 +44,12 @@ export interface EntitySource {
afterCreate?(ctx: CreateContext, rootId: EntityId): void;
}
-/** Category display order in the Create popover (mirrors the Add-Component taxonomy style). */
-export const CREATE_CATEGORY_ORDER = ['Basic', '2D', 'UI', 'Prefabs', 'Scripts'];
+/**
+ * Category display order in the Create popover. Reuses the Add-Component taxonomy
+ * (componentCategory) so the two pickers read as one system, plus create-only buckets:
+ * Basic (Empty), Prefabs (project assets), Scripts (user components).
+ */
+export const CREATE_CATEGORY_ORDER = ['Basic', 'Common', 'Rendering', 'Physics', 'Animation', 'UI', 'Audio', 'Effects', 'Prefabs', 'Scripts', 'Other'];
type CompSpec = string | [string, Record<string, unknown>];
@@ -105,12 +109,45 @@ const UI_WIDGET_ICON: Record<string, LucideIcon> = {
ListView: List,
};
+/**
+ * The entity-anchor components — each seeds a plain entity of that type. THE authority
+ * for "which components can start an entity": add an anchor here and it appears in the
+ * Create popover automatically, its category + icon drawn from the component-metadata
+ * authority (componentCategory / componentGlyph) so create and Add-Component read as one
+ * taxonomy. Attachment components (Collider/Joint/ShadowCaster2D/Velocity/UINode) are
+ * deliberately NOT anchors; TilemapLayer is asset-driven (createTilemap's tilesetSource)
+ * and Canvas needs a multi-component preset, so both are handled outside this table.
+ */
+const ANCHOR_SPECS: { comp: string; label: string; comps: CompSpec[] }[] = [
+ { comp: 'Sprite', label: 'Sprite', comps: ['Transform', 'Sprite'] },
+ { comp: 'Camera', label: 'Camera', comps: [['Transform', { position: { x: 0, y: 0, z: 10 } }], 'Camera'] },
+ { comp: 'ShapeRenderer', label: 'Shape', comps: ['Transform', 'ShapeRenderer'] },
+ { comp: 'Mesh2D', label: 'Mesh', comps: ['Transform', 'Mesh2D'] },
+ { comp: 'SpineAnimation', label: 'Spine', comps: ['Transform', 'SpineAnimation'] },
+ { comp: 'BitmapText', label: 'Bitmap Text', comps: ['Transform', 'BitmapText'] },
+ { comp: 'Text', label: 'Text', comps: ['Transform', 'Text'] },
+ { comp: 'ParticleEmitter', label: 'Particles', comps: ['Transform', 'ParticleEmitter'] },
+ { comp: 'TrailRenderer', label: 'Trail', comps: ['Transform', 'TrailRenderer'] },
+ { comp: 'Light2D', label: 'Light', comps: ['Transform', 'Light2D'] },
+ { comp: 'AudioSource', label: 'Audio', comps: ['Transform', 'AudioSource'] },
+];
+
+/** Auto-generate one source per anchor component; category + icon come from the
+ * component-metadata authority so a new anchor needs only a row in ANCHOR_SPECS. */
+function anchorSources(): EntitySource[] {
+ return ANCHOR_SPECS.map(({ comp, label, comps }) => ({
+ id: `anchor:${comp}`,
+ label,
+ category: componentCategory(comp),
+ icon: componentGlyph(comp),
+ keywords: [comp],
+ build: () => preset(label, comps),
+ }));
+}
+
export const ENTITY_SOURCES: EntitySource[] = [
presetSource('empty', 'Empty', 'Basic', CircleDot, ['Transform']),
- presetSource('sprite', 'Sprite', '2D', Image, ['Transform', 'Sprite']),
- presetSource('camera', 'Camera', '2D', Camera, [['Transform', { position: { x: 0, y: 0, z: 10 } }], 'Camera']),
- presetSource('particles', 'Particles', '2D', Sparkles, ['Transform', 'ParticleEmitter']),
- presetSource('light', 'Light', '2D', Lightbulb, ['Transform', 'Light2D']),
+ ...anchorSources(),
presetSource('canvas', 'Canvas', 'UI', LayoutPanelTop, ['Transform', ['Canvas', { designResolution: { x: 800, y: 600 }, scaleMode: 2 }], 'UINode']),
...BUILTIN_UI_WIDGET_NAMES.map((name): EntitySource => ({
id: `ui-${name.toLowerCase()}`,
diff --git a/desktop/tests/create-catalog.test.ts b/desktop/tests/create-catalog.test.ts
index 8394b138b..a8fb0ec04 100644
--- a/desktop/tests/create-catalog.test.ts
+++ b/desktop/tests/create-catalog.test.ts
@@ -1,14 +1,14 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
import { describe, it, expect } from 'vitest';
-import { ENTITY_SOURCES, matchSources } from '@/engine/entitySources';
+import { ENTITY_SOURCES, matchSources, CREATE_CATEGORY_ORDER } from '@/engine/entitySources';
describe('Create catalog (searchable source registry)', () => {
- it('spans Basic / 2D / UI, preserving order + carrying category', () => {
+ it('covers builtins + anchor components, every category in the declared order', () => {
expect(ENTITY_SOURCES.map((s) => s.label)).toEqual(
- expect.arrayContaining(['Empty', 'Sprite', 'Camera', 'Canvas', 'Button', 'Slider']),
+ expect.arrayContaining(['Empty', 'Sprite', 'Camera', 'Spine', 'Audio', 'Text', 'Canvas', 'Button']),
);
- expect(new Set(ENTITY_SOURCES.map((s) => s.category))).toEqual(new Set(['Basic', '2D', 'UI']));
+ for (const s of ENTITY_SOURCES) expect(CREATE_CATEGORY_ORDER).toContain(s.category);
});
it('builds one-entity presets from the registered component defaults', async () => {
@@ -25,8 +25,9 @@ describe('Create catalog (searchable source registry)', () => {
expect(matchSources(ENTITY_SOURCES, 'CAM').map((s) => s.label)).toEqual(['Camera']);
});
- it('matches on category label', () => {
- expect(matchSources(ENTITY_SOURCES, '2d').map((s) => s.label)).toEqual(['Sprite', 'Camera', 'Particles', 'Light']);
+ it('matches on category — create reuses the componentCategory taxonomy', () => {
+ const rendering = matchSources(ENTITY_SOURCES, 'rendering').map((s) => s.label);
+ expect(rendering).toEqual(expect.arrayContaining(['Sprite', 'Shape']));
});
it('empty query returns everything; no match returns nothing', () => {
diff --git a/desktop/tests/entity-sources.test.ts b/desktop/tests/entity-sources.test.ts
index 1ac8c52dc..5bf9b9c10 100644
--- a/desktop/tests/entity-sources.test.ts
+++ b/desktop/tests/entity-sources.test.ts
@@ -20,6 +20,17 @@ describe('entitySources registry (Create-entity E2)', () => {
}
});
+ it('every anchor component has a create source whose build carries that component (coverage guard)', async () => {
+ // The previously-missing anchors — the point of E6. If the engine gains a new anchor
+ // component, add it to ANCHOR_SPECS; this guards that the wired ones stay covered.
+ for (const comp of ['SpineAnimation', 'AudioSource', 'Text', 'BitmapText', 'ShapeRenderer', 'Mesh2D', 'TrailRenderer']) {
+ const src = ENTITY_SOURCES.find((s) => s.id === `anchor:${comp}`);
+ expect(src, `no create source for anchor ${comp}`).toBeDefined();
+ const p = await src!.build({ parent: null });
+ expect(p.entities[0].components.map((c) => c.type)).toContain(comp);
+ }
+ });
+
it('every source has a unique id, an icon, and a category in the declared order', () => {
const ids = ENTITY_SOURCES.map((s) => s.id);
expect(new Set(ids).size).toBe(ids.length);