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): route sprite creation through the unified create pipeline
esengine8d2e7656e5c7
diff --git a/desktop/src/engine/SceneCommands.ts b/desktop/src/engine/SceneCommands.tsindex 3394008f3..4041fa3cf 100644--- a/desktop/src/engine/SceneCommands.ts+++ b/desktop/src/engine/SceneCommands.ts@@ -383,42 +383,6 @@ export class SceneCommandsImpl {return sourceId;}- /**- * Spawn a Transform + Sprite entity at a world position, referencing `textureRef`- * (a `@uuid:` asset ref the Reconciler resolves to a handle) and sized to the- * texture's natural pixels. Drives the "drag an image into the viewport" flow.- * Undoable like {@link addEntity}.- */- addSpriteEntity(- name: string,- textureRef: string,- size: { x: number; y: number },- position: { x: number; y: number },- ): EntityId | null {- if (!this.model.current) return null;- const transform = structuredClone(DEFAULT_TRANSFORM);- transform.position = { x: position.x, y: position.y, z: 0 };- const spriteDef = componentByName('Sprite');- const sprite: Record<string, unknown> = spriteDef ? structuredClone(componentDefaults(spriteDef)) : {};- sprite.texture = textureRef;- sprite.size = { ...(sprite.size as object), x: size.x, y: size.y };- const sourceId = this.model.addEntity(name, [- { type: 'Transform', data: transform } as SceneComponent,- { type: 'Sprite', data: sprite } as SceneComponent,- ]);- let record: SceneEntity | undefined;- this.history.record(- `Add ${name}`,- () => {- if (record) this.model.restoreEntity(record);- },- () => {- record = this.model.removeEntityBySource(sourceId);- },- );- return sourceId;- }-/*** Delete an entity AND its descendants (the World despawns children with their* parent, so the model removes the whole subtree to stay consistent). Undodiff --git a/desktop/src/engine/entitySources.ts b/desktop/src/engine/entitySources.tsindex a4871da4c..bdfb061bf 100644--- a/desktop/src/engine/entitySources.ts+++ b/desktop/src/engine/entitySources.ts@@ -62,6 +62,15 @@ function preset(name: string, comps: CompSpec[]): PrefabData {};}+/**+ * A Transform + Sprite prefab referencing `textureRef`, sized to `size` (the model+ * side of "drag an image into the viewport"). Size is a plain vec2, so the preset+ * override replaces it cleanly. Instantiated through SceneCommands.create.+ */+export function spritePrefab(name: string, textureRef: string, size: { x: number; y: number }): PrefabData {+ return preset(name, [['Transform', {}], ['Sprite', { texture: textureRef, size: { x: size.x, y: size.y } }]]);+}+/** A static preset source — its prefab is built once from the registered component defaults. */function presetSource(id: string, label: string, category: string, icon: LucideIcon, comps: CompSpec[]): EntitySource {const prefab = preset(label, comps);diff --git a/desktop/src/project/ProjectStore.ts b/desktop/src/project/ProjectStore.tsindex 2db9ba10b..1f678c515 100644--- a/desktop/src/project/ProjectStore.ts+++ b/desktop/src/project/ProjectStore.ts@@ -11,6 +11,7 @@ import { blankInputMap } from './inputMapDoc';import { EditorHistory } from '@/engine/EditorHistory';import { expandScenePrefabs, collapseScenePrefabs } from '@/engine/PrefabInstance';import { SceneCommands } from '@/engine/SceneCommands';+import { spritePrefab } from '@/engine/entitySources';import { setPrefabBaseResolver } from '@/engine/SceneQuery';import { setUserSchemas, userSchema, setBitmaskSource, setEnumSource, type UserComponentSchema } from '@/engine/schema';import { installSpineSync, type SpineTransport } from '@/engine/spineSync';@@ -644,7 +645,7 @@ class ProjectStoreImpl {if (!ref) return null;const size = await this.imageNaturalSize(path);const name = (path.split('/').pop() ?? 'Sprite').replace(/\.[^.]+$/, '') || 'Sprite';- const id = SceneCommands.addSpriteEntity(name, ref, size, position);+ const id = SceneCommands.create(spritePrefab(name, ref, size), { parent: null, position });if (id != null) useSelection.getState().select(id);return id;}diff --git a/desktop/tests/add-sprite-entity.test.ts b/desktop/tests/add-sprite-entity.test.tsindex 5e2a73d04..84433f648 100644--- a/desktop/tests/add-sprite-entity.test.ts+++ b/desktop/tests/add-sprite-entity.test.ts@@ -1,20 +1,23 @@// SPDX-License-Identifier: Apache-2.0// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team/**- * @file SceneCommands.addSpriteEntity — the model side of "drag an image into the- * viewport": a Transform + Sprite entity at the drop point, texture ref set,- * sized to the texture, undoable. Pure TS (no WASM).+ * @file Sprite creation (REARCH ENTITY_CREATION E3) — the model side of "drag an+ * image into the viewport" now runs through the unified pipeline:+ * `spritePrefab(name, ref, size)` + SceneCommands.create at the drop point.+ * Proves the entity is a Transform + Sprite with the texture ref + size, and+ * one undo step — the same guarantees the bespoke addSpriteEntity gave. Pure TS.*/import { describe, it, expect, beforeEach } from 'vitest';import type { SceneData } from 'esengine';import { SceneModelImpl } from '@/engine/SceneModel';import { EditorHistoryImpl } from '@/engine/EditorHistory';import { SceneCommandsImpl } from '@/engine/SceneCommands';+import { spritePrefab } from '@/engine/entitySources';const emptyScene = (): SceneData =>({ version: '1.0', name: 't', entities: [] } as unknown as SceneData);-describe('SceneCommands.addSpriteEntity', () => {+describe('sprite creation (spritePrefab + SceneCommands.create)', () => {let model: SceneModelImpl;let history: EditorHistoryImpl;let cmds: SceneCommandsImpl;@@ -27,9 +30,9 @@ describe('SceneCommands.addSpriteEntity', () => {});it('creates a Transform + Sprite entity at the drop point with the texture ref + size', () => {- const id = cmds.addSpriteEntity('hero', '@uuid:abc', { x: 64, y: 48 }, { x: 120, y: -30 });+ const id = cmds.create(spritePrefab('hero', '@uuid:abc', { x: 64, y: 48 }), { parent: null, position: { x: 120, y: -30 } })!;expect(id).not.toBeNull();- const e = model.entityBySource(id!)!;+ const e = model.entityBySource(id)!;expect(e.name).toBe('hero');const tf = e.components.find((c) => c.type === 'Transform')!.data as { position: { x: number; y: number } };@@ -42,11 +45,11 @@ describe('SceneCommands.addSpriteEntity', () => {});it('is one undo step', () => {- const id = cmds.addSpriteEntity('hero', '@uuid:abc', { x: 10, y: 10 }, { x: 0, y: 0 });- expect(model.entityBySource(id!)).toBeTruthy();+ const id = cmds.create(spritePrefab('hero', '@uuid:abc', { x: 10, y: 10 }), { parent: null, position: { x: 0, y: 0 } })!;+ expect(model.entityBySource(id)).toBeTruthy();expect(history.canUndo()).toBe(true);history.undo();- expect(model.entityBySource(id!)).toBeUndefined();+ expect(model.entityBySource(id)).toBeUndefined();expect(history.canUndo()).toBe(false);});});