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.

refactor: introduce PropertyWritePipeline to replace handlePropertyChange switch

esengine091b8c8b5539
diff --git a/editor/src/__tests__/store/PropertyWritePipeline.test.ts b/editor/src/__tests__/store/PropertyWritePipeline.test.ts
new file mode 100644
index 000000000..876df3c25
--- /dev/null
+++ b/editor/src/__tests__/store/PropertyWritePipeline.test.ts
@@ -0,0 +1,228 @@
+import { describe, it, expect, beforeEach, vi } from 'vitest';
+import { PropertyWritePipeline, type PipelineHost } from '../../store/PropertyWritePipeline';
+import type { EntityData } from '../../types/SceneTypes';
+import type { PropertyChangeEvent } from '../../store/EditorStore';
+
+function makeEntityData(id: number, components: { type: string; data: Record<string, unknown> }[]): EntityData {
+ return {
+ id,
+ name: `Entity${id}`,
+ parent: null,
+ children: [],
+ visible: true,
+ components: components.map(c => ({ type: c.type, data: { ...c.data } })),
+ };
+}
+
+function makeEvent(
+ entity: number,
+ componentType: string,
+ propertyName: string,
+ oldValue: unknown,
+ newValue: unknown,
+): PropertyChangeEvent {
+ return { entity, componentType, propertyName, oldValue, newValue };
+}
+
+describe('PropertyWritePipeline', () => {
+ let pipeline: PropertyWritePipeline;
+ let entityMap: Map<number, EntityData>;
+ let writes: { entity: number; comp: string; prop: string; value: unknown }[];
+
+ beforeEach(() => {
+ entityMap = new Map();
+ writes = [];
+
+ const host: PipelineHost = {
+ getEntityData: (id) => entityMap.get(id),
+ writeDirect: (entity, comp, prop, value) => {
+ writes.push({ entity, comp, prop, value });
+ },
+ };
+ pipeline = new PropertyWritePipeline(host);
+ });
+
+ describe('transform hooks', () => {
+ it('runs specific property hook', () => {
+ const hook = vi.fn();
+ entityMap.set(1, makeEntityData(1, [{ type: 'Sprite', data: { color: 'red' } }]));
+
+ pipeline.registerTransformHook('Sprite', 'color', hook);
+ pipeline.handlePropertyNotification(makeEvent(1, 'Sprite', 'color', 'red', 'blue'));
+
+ expect(hook).toHaveBeenCalledOnce();
+ });
+
+ it('runs wildcard hook', () => {
+ const hook = vi.fn();
+ entityMap.set(1, makeEntityData(1, [{ type: 'Sprite', data: {} }]));
+
+ pipeline.registerTransformHook('Sprite', '*', hook);
+ pipeline.handlePropertyNotification(makeEvent(1, 'Sprite', 'size', null, { x: 1, y: 1 }));
+
+ expect(hook).toHaveBeenCalledOnce();
+ });
+
+ it('runs both specific and wildcard hooks', () => {
+ const specific = vi.fn();
+ const wildcard = vi.fn();
+ entityMap.set(1, makeEntityData(1, [{ type: 'Sprite', data: {} }]));
+
+ pipeline.registerTransformHook('Sprite', 'color', specific);
+ pipeline.registerTransformHook('Sprite', '*', wildcard);
+ pipeline.handlePropertyNotification(makeEvent(1, 'Sprite', 'color', 'red', 'blue'));
+
+ expect(specific).toHaveBeenCalledOnce();
+ expect(wildcard).toHaveBeenCalledOnce();
+ });
+
+ it('does not run hook for different component type', () => {
+ const hook = vi.fn();
+ entityMap.set(1, makeEntityData(1, [{ type: 'Transform', data: {} }]));
+
+ pipeline.registerTransformHook('Sprite', '*', hook);
+ pipeline.handlePropertyNotification(makeEvent(1, 'Transform', 'position', null, { x: 0, y: 0 }));
+
+ expect(hook).not.toHaveBeenCalled();
+ });
+
+ it('can issue additional writes via pipeline.writeDirect', () => {
+ entityMap.set(1, makeEntityData(1, [
+ { type: 'TextInput', data: { backgroundColor: 'white' } },
+ { type: 'Sprite', data: { color: 'white' } },
+ ]));
+
+ pipeline.registerTransformHook('TextInput', 'backgroundColor', (event, _ed, p) => {
+ p.writeDirect(event.entity, 'Sprite', 'color', event.newValue);
+ });
+
+ pipeline.handlePropertyNotification(
+ makeEvent(1, 'TextInput', 'backgroundColor', 'white', 'gray'),
+ );
+
+ expect(writes).toEqual([
+ { entity: 1, comp: 'Sprite', prop: 'color', value: 'gray' },
+ ]);
+ });
+
+ it('unsubscribes when dispose function called', () => {
+ const hook = vi.fn();
+ entityMap.set(1, makeEntityData(1, [{ type: 'Sprite', data: {} }]));
+
+ const unsub = pipeline.registerTransformHook('Sprite', 'color', hook);
+ unsub();
+
+ pipeline.handlePropertyNotification(makeEvent(1, 'Sprite', 'color', 'red', 'blue'));
+ expect(hook).not.toHaveBeenCalled();
+ });
+ });
+
+ describe('sync hooks', () => {
+ it('runs sync hooks after transform hooks', () => {
+ const order: string[] = [];
+ entityMap.set(1, makeEntityData(1, [{ type: 'Sprite', data: {} }]));
+
+ pipeline.registerTransformHook('Sprite', '*', () => { order.push('transform'); });
+ pipeline.registerSyncHook('Sprite', '*', () => { order.push('sync'); });
+
+ pipeline.handlePropertyNotification(makeEvent(1, 'Sprite', 'color', 'red', 'blue'));
+
+ expect(order).toEqual(['transform', 'sync']);
+ });
+
+ it('skips default sync when sync hook returns true', () => {
+ const defaultHook = vi.fn();
+ entityMap.set(1, makeEntityData(1, [{ type: 'Transform', data: {} }]));
+
+ pipeline.registerSyncHook('Transform', '*', () => true);
+ pipeline.setDefaultSyncHook(defaultHook);
+
+ pipeline.handlePropertyNotification(
+ makeEvent(1, 'Transform', 'position', { x: 0 }, { x: 1 }),
+ );
+
+ expect(defaultHook).not.toHaveBeenCalled();
+ });
+
+ it('runs default sync when no sync hook returns true', () => {
+ const defaultHook = vi.fn();
+ entityMap.set(1, makeEntityData(1, [{ type: 'Sprite', data: {} }]));
+
+ pipeline.setDefaultSyncHook(defaultHook);
+ pipeline.handlePropertyNotification(makeEvent(1, 'Sprite', 'color', 'red', 'blue'));
+
+ expect(defaultHook).toHaveBeenCalledOnce();
+ });
+
+ it('runs default sync when sync hook returns void', () => {
+ const defaultHook = vi.fn();
+ entityMap.set(1, makeEntityData(1, [{ type: 'UIRect', data: {} }]));
+
+ pipeline.registerSyncHook('UIRect', '*', () => { });
+ pipeline.setDefaultSyncHook(defaultHook);
+
+ pipeline.handlePropertyNotification(
+ makeEvent(1, 'UIRect', 'offsetMin', { x: 0 }, { x: 1 }),
+ );
+
+ expect(defaultHook).toHaveBeenCalledOnce();
+ });
+
+ it('unsubscribes when dispose function called', () => {
+ const hook = vi.fn();
+ entityMap.set(1, makeEntityData(1, [{ type: 'Sprite', data: {} }]));
+
+ const unsub = pipeline.registerSyncHook('Sprite', 'color', hook);
+ unsub();
+
+ pipeline.handlePropertyNotification(makeEvent(1, 'Sprite', 'color', 'red', 'blue'));
+ expect(hook).not.toHaveBeenCalled();
+ });
+ });
+
+ describe('entity data access', () => {
+ it('skips notification for unknown entity', () => {
+ const hook = vi.fn();
+ pipeline.registerTransformHook('Sprite', '*', hook);
+ pipeline.setDefaultSyncHook(hook);
+
+ pipeline.handlePropertyNotification(makeEvent(999, 'Sprite', 'color', 'red', 'blue'));
+
+ expect(hook).not.toHaveBeenCalled();
+ });
+
+ it('passes entityData to hooks', () => {
+ const entity = makeEntityData(1, [
+ { type: 'Transform', data: { position: { x: 0, y: 0 } } },
+ { type: 'UIRect', data: { offsetMin: { x: 0, y: 0 } } },
+ ]);
+ entityMap.set(1, entity);
+
+ pipeline.registerTransformHook('Transform', 'position', (_event, entityData) => {
+ expect(entityData.components).toHaveLength(2);
+ expect(entityData.components[1].type).toBe('UIRect');
+ });
+
+ pipeline.handlePropertyNotification(
+ makeEvent(1, 'Transform', 'position', { x: 0, y: 0 }, { x: 1, y: 1 }),
+ );
+ });
+ });
+
+ describe('setDefaultSyncHook', () => {
+ it('restores previous default when dispose called', () => {
+ const first = vi.fn();
+ const second = vi.fn();
+ entityMap.set(1, makeEntityData(1, [{ type: 'Sprite', data: {} }]));
+
+ pipeline.setDefaultSyncHook(first);
+ const unsub = pipeline.setDefaultSyncHook(second);
+ unsub();
+
+ pipeline.handlePropertyNotification(makeEvent(1, 'Sprite', 'color', 'red', 'blue'));
+
+ expect(first).toHaveBeenCalledOnce();
+ expect(second).not.toHaveBeenCalled();
+ });
+ });
+});
diff --git a/editor/src/__tests__/store/propertyHooks.test.ts b/editor/src/__tests__/store/propertyHooks.test.ts
new file mode 100644
index 000000000..e34823ab7
--- /dev/null
+++ b/editor/src/__tests__/store/propertyHooks.test.ts
@@ -0,0 +1,249 @@
+import { describe, it, expect, beforeEach } from 'vitest';
+import { PropertyWritePipeline, type PipelineHost } from '../../store/PropertyWritePipeline';
+import { registerBuiltinTransformHooks, type PropertyHookHost } from '../../store/propertyHooks';
+import type { EntityData, SceneData } from '../../types/SceneTypes';
+import type { PropertyChangeEvent } from '../../store/EditorStore';
+
+function makeEntityData(
+ id: number,
+ components: { type: string; data: Record<string, unknown> }[],
+ children: number[] = [],
+): EntityData {
+ return {
+ id,
+ name: `Entity${id}`,
+ parent: null,
+ children,
+ visible: true,
+ components: components.map(c => ({ type: c.type, data: { ...c.data } })),
+ };
+}
+
+function makeEvent(
+ entity: number,
+ componentType: string,
+ propertyName: string,
+ oldValue: unknown,
+ newValue: unknown,
+): PropertyChangeEvent {
+ return { entity, componentType, propertyName, oldValue, newValue };
+}
+
+describe('registerBuiltinTransformHooks', () => {
+ let pipeline: PropertyWritePipeline;
+ let entityMap: Map<number, EntityData>;
+ let writes: { entity: number; comp: string; prop: string; value: unknown }[];
+ let scene: SceneData;
+
+ beforeEach(() => {
+ entityMap = new Map();
+ writes = [];
+ scene = { name: 'Test', entities: [], settings: {} } as unknown as SceneData;
+
+ const pipelineHost: PipelineHost = {
+ getEntityData: (id) => entityMap.get(id),
+ writeDirect: (entity, comp, prop, value) => {
+ writes.push({ entity, comp, prop, value });
+ },
+ };
+ pipeline = new PropertyWritePipeline(pipelineHost);
+
+ const hookHost: PropertyHookHost = {
+ getEntityData: (id) => entityMap.get(id),
+ get scene() { return scene; },
+ };
+ registerBuiltinTransformHooks(pipeline, hookHost);
+ });
+
+ describe('Transform.position → UIRect redirect', () => {
+ it('redirects position delta to UIRect offsets when entity has UIRect', () => {
+ entityMap.set(1, makeEntityData(1, [
+ { type: 'Transform', data: { position: { x: 10, y: 20, z: 0 } } },
+ { type: 'UIRect', data: {
+ offsetMin: { x: -50, y: -25 },
+ offsetMax: { x: 50, y: 25 },
+ } },
+ ]));
+
+ pipeline.handlePropertyNotification(
+ makeEvent(1, 'Transform', 'position',
+ { x: 10, y: 20, z: 0 },
+ { x: 15, y: 30, z: 0 },
+ ),
+ );
+
+ expect(writes).toEqual([
+ { entity: 1, comp: 'UIRect', prop: 'offsetMin', value: { x: -45, y: -15 } },
+ { entity: 1, comp: 'UIRect', prop: 'offsetMax', value: { x: 55, y: 35 } },
+ ]);
+ });
+
+ it('does not redirect when entity has no UIRect', () => {
+ entityMap.set(1, makeEntityData(1, [
+ { type: 'Transform', data: { position: { x: 0, y: 0, z: 0 } } },
+ ]));
+
+ pipeline.handlePropertyNotification(
+ makeEvent(1, 'Transform', 'position',
+ { x: 0, y: 0, z: 0 },
+ { x: 5, y: 5, z: 0 },
+ ),
+ );
+
+ expect(writes).toEqual([]);
+ });
+
+ it('skips redirect when delta is zero', () => {
+ entityMap.set(1, makeEntityData(1, [
+ { type: 'Transform', data: { position: { x: 10, y: 20, z: 0 } } },
+ { type: 'UIRect', data: {
+ offsetMin: { x: 0, y: 0 },
+ offsetMax: { x: 100, y: 100 },
+ } },
+ ]));
+
+ pipeline.handlePropertyNotification(
+ makeEvent(1, 'Transform', 'position',
+ { x: 10, y: 20, z: 0 },
+ { x: 10, y: 20, z: 5 },
+ ),
+ );
+
+ expect(writes).toEqual([]);
+ });
+ });
+
+ describe('TextInput.backgroundColor → Sprite.color', () => {
+ it('syncs backgroundColor to Sprite.color', () => {
+ entityMap.set(1, makeEntityData(1, [
+ { type: 'TextInput', data: { backgroundColor: { r: 1, g: 1, b: 1, a: 1 } } },
+ { type: 'Sprite', data: { color: { r: 1, g: 1, b: 1, a: 1 } } },
+ ]));
+
+ const newColor = { r: 0.5, g: 0.5, b: 0.5, a: 1 };
+ pipeline.handlePropertyNotification(
+ makeEvent(1, 'TextInput', 'backgroundColor',
+ { r: 1, g: 1, b: 1, a: 1 },
+ newColor,
+ ),
+ );
+
+ expect(writes).toEqual([
+ { entity: 1, comp: 'Sprite', prop: 'color', value: newColor },
+ ]);
+ });
+
+ it('does not trigger for other TextInput properties', () => {
+ entityMap.set(1, makeEntityData(1, [
+ { type: 'TextInput', data: { text: '' } },
+ ]));
+
+ pipeline.handlePropertyNotification(
+ makeEvent(1, 'TextInput', 'text', '', 'hello'),
+ );
+
+ expect(writes).toEqual([]);
+ });
+ });
+
+ describe('Button → Sprite.color transition', () => {
+ it('syncs button transition color to Sprite.color', () => {
+ const normalColor = { r: 1, g: 1, b: 1, a: 1 };
+ const hoveredColor = { r: 0.8, g: 0.8, b: 0.8, a: 1 };
+ entityMap.set(1, makeEntityData(1, [
+ { type: 'Button', data: {
+ state: 0,
+ transition: {
+ normalColor,
+ hoveredColor,
+ pressedColor: { r: 0.6, g: 0.6, b: 0.6, a: 1 },
+ disabledColor: { r: 0.4, g: 0.4, b: 0.4, a: 1 },
+ },
+ } },
+ { type: 'Sprite', data: { color: { r: 1, g: 1, b: 1, a: 1 } } },
+ ]));
+
+ pipeline.handlePropertyNotification(
+ makeEvent(1, 'Button', 'state', 0, 0),
+ );
+
+ expect(writes).toHaveLength(1);
+ expect(writes[0].comp).toBe('Sprite');
+ expect(writes[0].prop).toBe('color');
+ expect(writes[0].value).toEqual(normalColor);
+ });
+
+ it('does not write when no transition data', () => {
+ entityMap.set(1, makeEntityData(1, [
+ { type: 'Button', data: { state: 0, transition: null } },
+ { type: 'Sprite', data: { color: { r: 1, g: 1, b: 1, a: 1 } } },
+ ]));
+
+ pipeline.handlePropertyNotification(
+ makeEvent(1, 'Button', 'state', 0, 1),
+ );
+
+ expect(writes).toEqual([]);
+ });
+
+ it('does not write when no Sprite component', () => {
+ entityMap.set(1, makeEntityData(1, [
+ { type: 'Button', data: {
+ state: 0,
+ transition: {
+ normalColor: { r: 1, g: 1, b: 1, a: 1 },
+ hoveredColor: { r: 0.8, g: 0.8, b: 0.8, a: 1 },
+ pressedColor: { r: 0.6, g: 0.6, b: 0.6, a: 1 },
+ disabledColor: { r: 0.4, g: 0.4, b: 0.4, a: 1 },
+ },
+ } },
+ ]));
+
+ pipeline.handlePropertyNotification(
+ makeEvent(1, 'Button', 'state', 0, 1),
+ );
+
+ expect(writes).toEqual([]);
+ });
+ });
+
+ describe('Canvas → Camera.orthoSize', () => {
+ it('syncs designResolution to Camera orthoSize', () => {
+ const cameraEntity = makeEntityData(2, [
+ { type: 'Camera', data: { orthoSize: 540 } },
+ ]);
+ entityMap.set(1, makeEntityData(1, [
+ { type: 'Canvas', data: { designResolution: { x: 1920, y: 1080 } } },
+ ]));
+ entityMap.set(2, cameraEntity);
+ scene.entities = [entityMap.get(1)!, cameraEntity];
+
+ pipeline.handlePropertyNotification(
+ makeEvent(1, 'Canvas', 'designResolution',
+ { x: 1280, y: 720 },
+ { x: 1920, y: 1080 },
+ ),
+ );
+
+ expect(writes).toEqual([
+ { entity: 2, comp: 'Camera', prop: 'orthoSize', value: 540 },
+ ]);
+ });
+
+ it('does nothing when no Camera entity exists', () => {
+ entityMap.set(1, makeEntityData(1, [
+ { type: 'Canvas', data: { designResolution: { x: 1920, y: 1080 } } },
+ ]));
+ scene.entities = [entityMap.get(1)!];
+
+ pipeline.handlePropertyNotification(
+ makeEvent(1, 'Canvas', 'designResolution',
+ { x: 1280, y: 720 },
+ { x: 1920, y: 1080 },
+ ),
+ );
+
+ expect(writes).toEqual([]);
+ });
+ });
+});
diff --git a/editor/src/renderer/EditorSceneRenderer.ts b/editor/src/renderer/EditorSceneRenderer.ts
index 001198b7c..1407fa1b9 100644
--- a/editor/src/renderer/EditorSceneRenderer.ts
+++ b/editor/src/renderer/EditorSceneRenderer.ts
@@ -2,7 +2,6 @@ import type { ESEngineModule, LayoutRect } from 'esengine';
import {
App,
UICameraInfo,
- ButtonState,
RenderPipeline,
Renderer,
PostProcess,
@@ -13,7 +12,7 @@ import {
} from 'esengine';
import type { SpineModuleController } from 'esengine/spine';
import type { SceneData, ComponentData } from '../types/SceneTypes';
-import type { EditorStore, PropertyChangeEvent } from '../store/EditorStore';
+import type { EditorStore } from '../store/EditorStore';
import { EditorCamera } from './EditorCamera';
import { EditorSceneManager } from '../scene/EditorSceneManager';
import { AssetPathResolver } from '../asset';
@@ -68,13 +67,17 @@ export class EditorSceneRenderer {
private setupSyncService(): void {
if (this.unsubscribes_.length > 0) return;
- if (!this.store_ || !this.sceneManager_) return;
+ if (!this.store_ || !this.sceneManager_) {
+ console.warn('[SceneRenderer] setupSyncService skipped: store=', !!this.store_, 'sceneManager=', !!this.sceneManager_);
+ return;
+ }
const store = this.store_;
const sm = this.sceneManager_;
+ this.registerPipelineSyncHooks_(store);
+
this.unsubscribes_.push(
- store.subscribeToPropertyChanges((e) => this.handlePropertyChange(e)),
store.subscribeToHierarchyChanges((e) => {
sm.reparentEntity(e.entity, e.newParent);
}),
@@ -127,93 +130,48 @@ export class EditorSceneRenderer {
'position', 'rotation', 'scale',
]);
- private handlePropertyChange(event: PropertyChangeEvent): void {
- if (!this.sceneManager_?.hasEntity(event.entity)) return;
-
- switch (event.componentType) {
- case 'UIRect':
- this.scheduleDescendantUpdates(event.entity);
- break;
- case 'Canvas':
- this.scheduleDescendantUpdates(event.entity);
- this.syncCanvasToCamera(event.entity);
- break;
- case 'Button':
- this.syncButtonTransitionColor(event.entity);
- break;
- case 'TextInput':
- if (event.propertyName === 'backgroundColor') {
- this.store_!.updatePropertyDirect(event.entity, 'Sprite', 'color', event.newValue);
- }
- break;
- case 'PostProcessVolume':
- this.syncPostProcessVolumeForEntity(event.entity);
- break;
- }
-
- if (event.componentType === 'Transform'
- && EditorSceneRenderer.TRANSFORM_DIRECT_PROPS_.has(event.propertyName)) {
- const entityData = this.store_!.getEntityData(event.entity);
- const transform = entityData?.components.find(c => c.type === 'Transform');
- if (transform) {
- this.sceneManager_!.updateTransform(event.entity, transform.data);
- this.renderCallback_?.();
- }
- return;
- }
+ private registerPipelineSyncHooks_(store: EditorStore): void {
+ const pipeline = store.pipeline_;
- this.scheduleEntityUpdate(event.entity);
- }
-
- private syncCanvasToCamera(canvasEntityId: number): void {
- const canvasData = this.store_!.getEntityData(canvasEntityId);
- if (!canvasData) return;
-
- const canvasComp = canvasData.components.find(c => c.type === 'Canvas');
- const resolution = canvasComp?.data?.designResolution as { x: number; y: number } | undefined;
- if (!resolution) return;
-
- const orthoSize = resolution.y / 2;
+ this.unsubscribes_.push(
+ pipeline.registerSyncHook('Transform', '*', (event, entityData) => {
+ if (!this.sceneManager_?.hasEntity(event.entity)) return;
- for (const entity of this.store_!.scene.entities) {
- const cameraComp = entity.components.find(c => c.type === 'Camera');
- if (!cameraComp) continue;
+ if (EditorSceneRenderer.TRANSFORM_DIRECT_PROPS_.has(event.propertyName)) {
+ if (event.propertyName === 'position'
+ && entityData.components.some(c => c.type === 'UIRect')) {
+ this.scheduleEntityUpdate(event.entity);
+ } else {
+ const transform = entityData.components.find(c => c.type === 'Transform');
+ if (transform) {
+ this.sceneManager_!.updateTransform(event.entity, transform.data);
+ }
+ }
+ this.renderCallback_?.();
+ return true;
+ }
+ }),
- this.store_!.updatePropertyDirect(entity.id, 'Camera', 'orthoSize', orthoSize);
- this.scheduleEntityUpdate(entity.id);
- break;
- }
- }
+ pipeline.registerSyncHook('UIRect', '*', (event) => {
+ if (!this.sceneManager_?.hasEntity(event.entity)) return;
+ this.scheduleDescendantUpdates(event.entity);
+ }),
- private syncButtonTransitionColor(entityId: number): void {
- const entityData = this.store_!.getEntityData(entityId);
- if (!entityData) return;
+ pipeline.registerSyncHook('Canvas', '*', (event) => {
+ if (!this.sceneManager_?.hasEntity(event.entity)) return;
+ this.scheduleDescendantUpdates(event.entity);
+ }),
- const buttonComp = entityData.components.find(c => c.type === 'Button');
- if (!buttonComp) return;
-
- const transition = buttonComp.data.transition as {
- normalColor: { r: number; g: number; b: number; a: number };
- hoveredColor: { r: number; g: number; b: number; a: number };
- pressedColor: { r: number; g: number; b: number; a: number };
- disabledColor: { r: number; g: number; b: number; a: number };
- } | null;
- if (!transition) return;
-
- const spriteComp = entityData.components.find(c => c.type === 'Sprite');
- if (!spriteComp) return;
-
- const state = (buttonComp.data.state as number) ?? ButtonState.Normal;
- const colorMap: Record<number, { r: number; g: number; b: number; a: number }> = {
- [ButtonState.Normal]: transition.normalColor,
- [ButtonState.Hovered]: transition.hoveredColor,
- [ButtonState.Pressed]: transition.pressedColor,
- [ButtonState.Disabled]: transition.disabledColor,
- };
- const color = colorMap[state] ?? transition.normalColor;
+ pipeline.registerSyncHook('PostProcessVolume', '*', (event) => {
+ if (!this.sceneManager_?.hasEntity(event.entity)) return;
+ this.syncPostProcessVolumeForEntity(event.entity);
+ }),
- this.store_!.updatePropertyDirect(entityId, 'Sprite', 'color', { ...color });
- this.scheduleEntityUpdate(entityId);
+ pipeline.setDefaultSyncHook((event) => {
+ if (!this.sceneManager_?.hasEntity(event.entity)) return;
+ this.scheduleEntityUpdate(event.entity);
+ }),
+ );
}
private scheduleDescendantUpdates(entityId: number): void {
diff --git a/editor/src/store/EditorStore.ts b/editor/src/store/EditorStore.ts
index a6952c00c..0d343c644 100644
--- a/editor/src/store/EditorStore.ts
+++ b/editor/src/store/EditorStore.ts
@@ -7,6 +7,8 @@ import type { TransformValue } from '../math/Transform';
import { SelectionService } from './SelectionService';
import { SceneOperations } from './SceneOperations';
import { PrefabEditService } from './PrefabEditService';
+import { PropertyWritePipeline } from './PropertyWritePipeline';
+import { registerBuiltinTransformHooks } from './propertyHooks';
// =============================================================================
// Types
@@ -106,6 +108,7 @@ export class EditorStore {
private readonly selection_: SelectionService;
private readonly sceneOps_: SceneOperations;
private readonly prefabEdit_: PrefabEditService;
+ readonly pipeline_: PropertyWritePipeline;
private static readonly AUTOSAVE_KEY = 'esengine.autosave';
private static readonly AUTOSAVE_INTERVAL = 60_000;
@@ -128,6 +131,17 @@ export class EditorStore {
this.sceneOps_ = new SceneOperations(this);
this.prefabEdit_ = new PrefabEditService(this);
+ this.pipeline_ = new PropertyWritePipeline({
+ getEntityData: (id: number) => this.entityMap_.get(id),
+ writeDirect: (entity, comp, prop, val) =>
+ this.sceneOps_.updatePropertyDirect(entity as Entity, comp, prop, val),
+ });
+ const self = this;
+ registerBuiltinTransformHooks(this.pipeline_, {
+ getEntityData: (id: number) => self.entityMap_.get(id),
+ get scene() { return self.state_.scene; },
+ });
+
this.startAutoSave();
}
@@ -616,6 +630,7 @@ export class EditorStore {
}
notifyPropertyChange(event: PropertyChangeEvent): void {
+ this.pipeline_.handlePropertyNotification(event);
for (const listener of this.propertyListeners_) {
listener(event);
}
diff --git a/editor/src/store/PropertyWritePipeline.ts b/editor/src/store/PropertyWritePipeline.ts
new file mode 100644
index 000000000..790fdbaeb
--- /dev/null
+++ b/editor/src/store/PropertyWritePipeline.ts
@@ -0,0 +1,118 @@
+import type { EntityData } from '../types/SceneTypes';
+import type { PropertyChangeEvent } from './EditorStore';
+
+export interface WriteRequest {
+ entity: number;
+ componentType: string;
+ propertyName: string;
+ newValue: unknown;
+}
+
+export interface PipelineHost {
+ getEntityData(entityId: number): EntityData | undefined;
+ writeDirect(entity: number, componentType: string, propertyName: string, newValue: unknown): void;
+}
+
+export type TransformHook = (
+ event: PropertyChangeEvent,
+ entityData: EntityData,
+ pipeline: PropertyWritePipeline,
+) => void;
+
+export type SyncHook = (
+ event: PropertyChangeEvent,
+ entityData: EntityData,
+) => boolean | void;
+
+type HookKey = string;
+
+export class PropertyWritePipeline {
+ private transformHooks_ = new Map<HookKey, TransformHook[]>();
+ private syncHooks_ = new Map<HookKey, SyncHook[]>();
+ private defaultSyncHook_: SyncHook | null = null;
+ private host_: PipelineHost;
+
+ constructor(host: PipelineHost) {
+ this.host_ = host;
+ }
+
+ registerTransformHook(
+ componentType: string,
+ propertyName: string | '*',
+ hook: TransformHook,
+ ): () => void {
+ const key = propertyName === '*' ? componentType : `${componentType}.${propertyName}`;
+ let hooks = this.transformHooks_.get(key);
+ if (!hooks) {
+ hooks = [];
+ this.transformHooks_.set(key, hooks);
+ }
+ hooks.push(hook);
+ return () => {
+ const idx = hooks!.indexOf(hook);
+ if (idx >= 0) hooks!.splice(idx, 1);
+ };
+ }
+
+ registerSyncHook(
+ componentType: string,
+ propertyName: string | '*',
+ hook: SyncHook,
+ ): () => void {
+ const key = propertyName === '*' ? componentType : `${componentType}.${propertyName}`;
+ let hooks = this.syncHooks_.get(key);
+ if (!hooks) {
+ hooks = [];
+ this.syncHooks_.set(key, hooks);
+ }
+ hooks.push(hook);
+ return () => {
+ const idx = hooks!.indexOf(hook);
+ if (idx >= 0) hooks!.splice(idx, 1);
+ };
+ }
+
+ setDefaultSyncHook(hook: SyncHook): () => void {
+ const prev = this.defaultSyncHook_;
+ this.defaultSyncHook_ = hook;
+ return () => { this.defaultSyncHook_ = prev; };
+ }
+
+ writeDirect(
+ entity: number,
+ componentType: string,
+ propertyName: string,
+ newValue: unknown,
+ ): void {
+ this.host_.writeDirect(entity, componentType, propertyName, newValue);
+ }
+
+ handlePropertyNotification(event: PropertyChangeEvent): void {
+ const entityData = this.host_.getEntityData(event.entity);
+ if (!entityData) return;
+
+ const specificKey = `${event.componentType}.${event.propertyName}`;
+ const wildcardKey = event.componentType;
+
+ for (const key of [specificKey, wildcardKey]) {
+ const hooks = this.transformHooks_.get(key);
+ if (hooks) {
+ for (const hook of hooks) hook(event, entityData, this);
+ }
+ }
+
+ let handled = false;
+ for (const key of [specificKey, wildcardKey]) {
+ const hooks = this.syncHooks_.get(key);
+ if (hooks) {
+ for (const hook of hooks) {
+ if (hook(event, entityData) === true) handled = true;
+ }
+ }
+ }
+
+ if (!handled && this.defaultSyncHook_) {
+ this.defaultSyncHook_(event, entityData);
+ }
+ }
+}
diff --git a/editor/src/store/propertyHooks.ts b/editor/src/store/propertyHooks.ts
new file mode 100644
index 000000000..c96a7004b
--- /dev/null
+++ b/editor/src/store/propertyHooks.ts
@@ -0,0 +1,80 @@
+import { ButtonState } from 'esengine';
+import type { EntityData, SceneData } from '../types/SceneTypes';
+import type { PropertyWritePipeline } from './PropertyWritePipeline';
+
+export interface PropertyHookHost {
+ getEntityData(entityId: number): EntityData | undefined;
+ readonly scene: SceneData;
+}
+
+export function registerBuiltinTransformHooks(
+ pipeline: PropertyWritePipeline,
+ host: PropertyHookHost,
+): void {
+ pipeline.registerTransformHook('Transform', 'position', (event, entityData, p) => {
+ if (!entityData.components.some(c => c.type === 'UIRect')) return;
+
+ const uiRect = entityData.components.find(c => c.type === 'UIRect');
+ if (!uiRect) return;
+
+ const oldPos = event.oldValue as { x: number; y: number };
+ const newPos = event.newValue as { x: number; y: number };
+ const dx = newPos.x - oldPos.x;
+ const dy = newPos.y - oldPos.y;
+ if (dx === 0 && dy === 0) return;
+
+ const offsetMin = uiRect.data.offsetMin as { x: number; y: number };
+ const offsetMax = uiRect.data.offsetMax as { x: number; y: number };
+
+ p.writeDirect(event.entity, 'UIRect', 'offsetMin', {
+ x: offsetMin.x + dx, y: offsetMin.y + dy,
+ });
+ p.writeDirect(event.entity, 'UIRect', 'offsetMax', {
+ x: offsetMax.x + dx, y: offsetMax.y + dy,
+ });
+ });
+
+ pipeline.registerTransformHook('TextInput', 'backgroundColor', (event, _entityData, p) => {
+ p.writeDirect(event.entity, 'Sprite', 'color', event.newValue);
+ });
+
+ pipeline.registerTransformHook('Button', '*', (event, entityData, p) => {
+ const buttonComp = entityData.components.find(c => c.type === 'Button');
+ const spriteComp = entityData.components.find(c => c.type === 'Sprite');
+ if (!buttonComp || !spriteComp) return;
+
+ const transition = buttonComp.data.transition as {
+ normalColor: { r: number; g: number; b: number; a: number };
+ hoveredColor: { r: number; g: number; b: number; a: number };
+ pressedColor: { r: number; g: number; b: number; a: number };
+ disabledColor: { r: number; g: number; b: number; a: number };
+ } | null;
+ if (!transition) return;
+
+ const state = (buttonComp.data.state as number) ?? ButtonState.Normal;
+ const colorMap: Record<number, { r: number; g: number; b: number; a: number }> = {
+ [ButtonState.Normal]: transition.normalColor,
+ [ButtonState.Hovered]: transition.hoveredColor,
+ [ButtonState.Pressed]: transition.pressedColor,
+ [ButtonState.Disabled]: transition.disabledColor,
+ };
+ const color = colorMap[state] ?? transition.normalColor;
+
+ p.writeDirect(event.entity, 'Sprite', 'color', { ...color });
+ });
+
+ pipeline.registerTransformHook('Canvas', '*', (event, entityData, p) => {
+ const canvasComp = entityData.components.find(c => c.type === 'Canvas');
+ const resolution = canvasComp?.data?.designResolution as { x: number; y: number } | undefined;
+ if (!resolution) return;
+
+ const orthoSize = resolution.y / 2;
+
+ for (const entity of host.scene.entities) {
+ const cameraComp = entity.components.find(c => c.type === 'Camera');
+ if (!cameraComp) continue;
+ p.writeDirect(entity.id, 'Camera', 'orthoSize', orthoSize);
+ break;
+ }
+ });
+}