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): user components appear in the Create popover (E4a)

esenginec1c5b222884d
diff --git a/desktop/src/components/CreatePopover.tsx b/desktop/src/components/CreatePopover.tsx
index 3ff6eefd1..de58daa21 100644
--- a/desktop/src/components/CreatePopover.tsx
+++ b/desktop/src/components/CreatePopover.tsx
@@ -12,7 +12,7 @@
*/
import { useEffect, useMemo, useRef, useState } from 'react';
import { createPortal } from 'react-dom';
-import { ENTITY_SOURCES, matchSources, type EntitySource } from '@/engine/entitySources';
+import { ENTITY_SOURCES, userComponentSources, matchSources, type EntitySource } from '@/engine/entitySources';
import { SearchField } from '@/components/SearchField';
import { useDialogFocus } from '@/components/dialogFocus';
@@ -44,7 +44,9 @@ export function CreatePopover({
const shellRef = useRef<HTMLDivElement>(null);
useDialogFocus(shellRef);
- const results = useMemo(() => matchSources(ENTITY_SOURCES, query), [query]);
+ // Static builtins + the user's own components; computed once when the popover opens.
+ const all = useMemo(() => [...ENTITY_SOURCES, ...userComponentSources()], []);
+ const results = useMemo(() => matchSources(all, query), [all, query]);
const groups = useMemo(() => groupByCategory(results), [results]);
useEffect(() => inputRef.current?.focus(), []);
diff --git a/desktop/src/components/icons.tsx b/desktop/src/components/icons.tsx
index d1c04d0bc..f45338ee8 100644
--- a/desktop/src/components/icons.tsx
+++ b/desktop/src/components/icons.tsx
@@ -64,7 +64,7 @@ const COMPONENT_ICON: Record<string, LucideIcon> = {
RigidBody: Box,
};
-function componentGlyph(name: string): LucideIcon {
+export function componentGlyph(name: string): LucideIcon {
const hit = COMPONENT_ICON[name];
if (hit) return hit;
if (/Collider$/.test(name)) return Hexagon;
diff --git a/desktop/src/engine/entitySources.ts b/desktop/src/engine/entitySources.ts
index 5ceceebc6..0bab1f23b 100644
--- a/desktop/src/engine/entitySources.ts
+++ b/desktop/src/engine/entitySources.ts
@@ -12,9 +12,10 @@
*/
import type { LucideIcon } from 'lucide-react';
import { CircleDot, Image, Camera, Sparkles, Lightbulb, LayoutPanelTop, ToggleLeft, SlidersHorizontal, List, ChevronDown, SquareMousePointer, RectangleHorizontal, Box } from 'lucide-react';
-import { BUILTIN_UI_PREFABS, BUILTIN_UI_WIDGET_NAMES, PREFAB_FORMAT_VERSION, type PrefabData } from 'esengine';
+import { BUILTIN_UI_PREFABS, BUILTIN_UI_WIDGET_NAMES, PREFAB_FORMAT_VERSION, getUserComponents, type PrefabData } from 'esengine';
import type { EntityId } from '@/types';
-import { componentByName, componentDefaults } from './schema';
+import { componentByName, componentDefaults, prettyLabel } from './schema';
+import { componentGlyph } from '@/components/icons';
import { SceneCommands } from './SceneCommands';
/** Where a source is created: the target parent + (for asset/drop sources) its origin. */
@@ -114,6 +115,23 @@ export const ENTITY_SOURCES: EntitySource[] = [
})),
];
+/**
+ * Create-entity sources for the user's own components (defineComponent): a plain
+ * entity carrying Transform + that component, bucketed under 'Scripts'. A
+ * user-defined component thus appears in the Create popover with zero extra wiring
+ * — the dynamic half of "cover all cases" (REARCH ENTITY_CREATION E4).
+ */
+export function userComponentSources(): EntitySource[] {
+ return [...getUserComponents().keys()].map((name) => ({
+ id: `component:${name}`,
+ label: prettyLabel(name),
+ category: 'Scripts',
+ icon: componentGlyph(name),
+ keywords: [name],
+ build: () => preset(name, ['Transform', name]),
+ }));
+}
+
/** Case-insensitive filter over label + category + keywords, for the Create popover. */
export function matchSources(sources: EntitySource[], query: string): EntitySource[] {
const q = query.trim().toLowerCase();
diff --git a/desktop/tests/user-component-sources.test.ts b/desktop/tests/user-component-sources.test.ts
new file mode 100644
index 000000000..4e507edd3
--- /dev/null
+++ b/desktop/tests/user-component-sources.test.ts
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file userComponentSources (REARCH ENTITY_CREATION E4) — a user's defineComponent
+ * components appear in the Create popover as plain Transform + that-component
+ * entities, bucketed under 'Scripts', with zero per-component wiring. Pure TS;
+ * getUserComponents is mocked to a deterministic set.
+ */
+import { describe, it, expect, vi } from 'vitest';
+
+vi.mock('esengine', async (importOriginal) => {
+ const actual = (await importOriginal()) as Record<string, unknown>;
+ return { ...actual, getUserComponents: () => new Map([['Health', {}], ['Inventory', {}]]) };
+});
+
+import { userComponentSources } from '@/engine/entitySources';
+
+describe('userComponentSources (Create-entity E4 dynamic sources)', () => {
+ it('makes one Scripts-category source per user component', () => {
+ const srcs = userComponentSources();
+ expect(srcs.map((s) => s.label)).toEqual(expect.arrayContaining(['Health', 'Inventory']));
+ expect(srcs).toHaveLength(2);
+ for (const s of srcs) {
+ expect(s.category).toBe('Scripts');
+ expect(s.icon).toBeTruthy();
+ }
+ });
+
+ it('build() yields a plain entity carrying Transform + the component', async () => {
+ const health = userComponentSources().find((s) => s.label === 'Health')!;
+ const p = await health.build({ parent: null });
+ expect(p.entities[0].components.map((c) => c.type)).toEqual(['Transform', 'Health']);
+ });
+});