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: unify command change notifications and panel update paths

esengine438fb870be6e
diff --git a/editor/src/commands/Command.ts b/editor/src/commands/Command.ts
index 1842a7b6c..9d5d15713 100644
--- a/editor/src/commands/Command.ts
+++ b/editor/src/commands/Command.ts
@@ -5,6 +5,25 @@
import type { EntityData } from '../types/SceneTypes';
+// =============================================================================
+// ChangeEmitter Interface
+// =============================================================================
+
+export interface ChangeEmitter {
+ notifyPropertyChange(event: {
+ entity: number; componentType: string; propertyName: string;
+ oldValue: unknown; newValue: unknown;
+ }): void;
+ notifyVisibilityChange(event: { entity: number; visible: boolean }): void;
+ notifyHierarchyChange(event: { entity: number; newParent: number | null }): void;
+ notifyEntityLifecycle(event: {
+ entity: number; type: 'created' | 'deleted'; parent: number | null;
+ }): void;
+ notifyComponentChange(event: {
+ entity: number; componentType: string; action: 'added' | 'removed';
+ }): void;
+}
+
// =============================================================================
// Command Interface
// =============================================================================
@@ -23,6 +42,7 @@ export interface Command {
merge(other: Command): Command;
updateEntityMap(map: Map<number, EntityData>, isUndo: boolean): void;
+ emitChangeEvents(emitter: ChangeEmitter, isUndo: boolean): void;
}
// =============================================================================
@@ -55,4 +75,5 @@ export abstract class BaseCommand implements Command {
}
updateEntityMap(_map: Map<number, EntityData>, _isUndo: boolean): void {}
+ emitChangeEvents(_emitter: ChangeEmitter, _isUndo: boolean): void {}
}
diff --git a/editor/src/commands/CompoundCommand.ts b/editor/src/commands/CompoundCommand.ts
index 4cc1c9594..149f76b6a 100644
--- a/editor/src/commands/CompoundCommand.ts
+++ b/editor/src/commands/CompoundCommand.ts
@@ -4,7 +4,7 @@
*/
import type { EntityData } from '../types/SceneTypes';
-import { BaseCommand, type Command } from './Command';
+import { BaseCommand, type ChangeEmitter, type Command } from './Command';
export class CompoundCommand extends BaseCommand {
readonly type = 'compound';
@@ -17,6 +17,10 @@ export class CompoundCommand extends BaseCommand {
this.structural = commands_.some(c => c.structural);
}
+ get commands(): readonly Command[] {
+ return this.commands_;
+ }
+
execute(): void {
for (const cmd of this.commands_) {
cmd.execute();
@@ -40,4 +44,16 @@ export class CompoundCommand extends BaseCommand {
}
}
}
+
+ emitChangeEvents(emitter: ChangeEmitter, isUndo: boolean): void {
+ if (isUndo) {
+ for (let i = this.commands_.length - 1; i >= 0; i--) {
+ this.commands_[i].emitChangeEvents(emitter, isUndo);
+ }
+ } else {
+ for (const cmd of this.commands_) {
+ cmd.emitChangeEvents(emitter, isUndo);
+ }
+ }
+ }
}
diff --git a/editor/src/commands/EntityCommands.ts b/editor/src/commands/EntityCommands.ts
index 8856b6fe5..2ac9c9aec 100644
--- a/editor/src/commands/EntityCommands.ts
+++ b/editor/src/commands/EntityCommands.ts
@@ -5,7 +5,7 @@
import type { SceneData, EntityData } from '../types/SceneTypes';
import { createEntityData } from '../types/SceneTypes';
-import { BaseCommand } from './Command';
+import { BaseCommand, type ChangeEmitter } from './Command';
// =============================================================================
// CreateEntityCommand
@@ -65,6 +65,14 @@ export class CreateEntityCommand extends BaseCommand {
}
}
+ emitChangeEvents(emitter: ChangeEmitter, isUndo: boolean): void {
+ emitter.notifyEntityLifecycle({
+ entity: this.entityId_,
+ type: isUndo ? 'deleted' : 'created',
+ parent: this.parent_,
+ });
+ }
+
get entityId(): number {
return this.entityId_;
}
@@ -125,6 +133,24 @@ export class DeleteEntityCommand extends BaseCommand {
}
}
+ emitChangeEvents(emitter: ChangeEmitter, isUndo: boolean): void {
+ if (isUndo) {
+ emitter.notifyEntityLifecycle({
+ entity: this.entityId_,
+ type: 'created',
+ parent: this.deletedData_?.parent ?? null,
+ });
+ for (const child of this.deletedChildren_) {
+ emitter.notifyEntityLifecycle({ entity: child.id, type: 'created', parent: child.parent });
+ }
+ } else {
+ for (const child of this.deletedChildren_) {
+ emitter.notifyEntityLifecycle({ entity: child.id, type: 'deleted', parent: null });
+ }
+ emitter.notifyEntityLifecycle({ entity: this.entityId_, type: 'deleted', parent: null });
+ }
+ }
+
updateEntityMap(map: Map<number, EntityData>, isUndo: boolean): void {
if (isUndo) {
if (this.deletedData_) {
@@ -187,6 +213,13 @@ export class ReparentCommand extends BaseCommand {
this.setParent(this.oldParent_);
}
+ emitChangeEvents(emitter: ChangeEmitter, isUndo: boolean): void {
+ emitter.notifyHierarchyChange({
+ entity: this.entityId_,
+ newParent: isUndo ? this.oldParent_ : this.newParent_,
+ });
+ }
+
private setParent(newParent: number | null): void {
const entity = this.entityMap_.get(this.entityId_);
if (!entity) return;
@@ -247,6 +280,13 @@ export class MoveEntityCommand extends BaseCommand {
this.applyMove(this.oldParent_, this.oldIndex_);
}
+ emitChangeEvents(emitter: ChangeEmitter, isUndo: boolean): void {
+ emitter.notifyHierarchyChange({
+ entity: this.entityId_,
+ newParent: isUndo ? this.oldParent_ : this.newParent_,
+ });
+ }
+
private computeIndex(parent: number | null): number {
if (parent !== null) {
const parentData = this.entityMap_.get(parent);
@@ -330,6 +370,14 @@ export class AddComponentCommand extends BaseCommand {
entity.components.splice(idx, 1);
}
}
+
+ emitChangeEvents(emitter: ChangeEmitter, isUndo: boolean): void {
+ emitter.notifyComponentChange({
+ entity: this.entityId_,
+ componentType: this.componentType_,
+ action: isUndo ? 'removed' : 'added',
+ });
+ }
}
// =============================================================================
@@ -373,6 +421,14 @@ export class RemoveComponentCommand extends BaseCommand {
data: this.removedData_,
});
}
+
+ emitChangeEvents(emitter: ChangeEmitter, isUndo: boolean): void {
+ emitter.notifyComponentChange({
+ entity: this.entityId_,
+ componentType: this.componentType_,
+ action: isUndo ? 'added' : 'removed',
+ });
+ }
}
// =============================================================================
diff --git a/editor/src/commands/PropertyCommand.ts b/editor/src/commands/PropertyCommand.ts
index 0053ac052..cf1c4229a 100644
--- a/editor/src/commands/PropertyCommand.ts
+++ b/editor/src/commands/PropertyCommand.ts
@@ -5,7 +5,7 @@
import type { Entity } from 'esengine';
import type { SceneData, EntityData } from '../types/SceneTypes';
-import { BaseCommand, type Command } from './Command';
+import { BaseCommand, type ChangeEmitter, type Command } from './Command';
// =============================================================================
// Constants
@@ -63,10 +63,28 @@ export class PropertyCommand extends BaseCommand {
);
}
+ emitChangeEvents(emitter: ChangeEmitter, isUndo: boolean): void {
+ emitter.notifyPropertyChange({
+ entity: this.entity_ as number,
+ componentType: this.componentType_,
+ propertyName: this.propertyName_,
+ oldValue: isUndo ? this.newValue_ : this.oldValue_,
+ newValue: isUndo ? this.oldValue_ : this.newValue_,
+ });
+ }
+
+ get entity(): number {
+ return this.entity_ as number;
+ }
+
get componentType(): string {
return this.componentType_;
}
+ get propertyName(): string {
+ return this.propertyName_;
+ }
+
private setPropertyValue(value: unknown): void {
const entityData = this.entityMap_.get(this.entity_ as number);
if (!entityData) return;
diff --git a/editor/src/commands/ToggleVisibilityCommand.ts b/editor/src/commands/ToggleVisibilityCommand.ts
index a2bc38b04..9cb5c7ade 100644
--- a/editor/src/commands/ToggleVisibilityCommand.ts
+++ b/editor/src/commands/ToggleVisibilityCommand.ts
@@ -1,5 +1,5 @@
import type { EntityData, SceneData } from '../types/SceneTypes';
-import { BaseCommand } from './Command';
+import { BaseCommand, type ChangeEmitter } from './Command';
export class ToggleVisibilityCommand extends BaseCommand {
readonly type = 'toggle_visibility';
@@ -9,8 +9,7 @@ export class ToggleVisibilityCommand extends BaseCommand {
constructor(
private scene_: SceneData,
private entityMap_: Map<number, EntityData>,
- private entityId_: number,
- private visibilityCallback_: (entityId: number, visible: boolean) => void
+ private entityId_: number
) {
super();
const entityData = entityMap_.get(entityId_);
@@ -42,7 +41,15 @@ export class ToggleVisibilityCommand extends BaseCommand {
const entityData = this.entityMap_.get(id);
if (entityData) {
entityData.visible = visible;
- this.visibilityCallback_(id, visible);
+ }
+ }
+ }
+
+ emitChangeEvents(emitter: ChangeEmitter, _isUndo: boolean): void {
+ for (const [id] of this.savedStates_) {
+ const entityData = this.entityMap_.get(id);
+ if (entityData) {
+ emitter.notifyVisibilityChange({ entity: id, visible: entityData.visible });
}
}
}
@@ -60,7 +67,6 @@ export class ToggleVisibilityCommand extends BaseCommand {
const entityData = this.entityMap_.get(entityId);
if (!entityData) return;
entityData.visible = false;
- this.visibilityCallback_(entityId, false);
for (const childId of entityData.children) {
const childData = this.entityMap_.get(childId);
if (childData && childData.visible !== false) {
@@ -73,7 +79,6 @@ export class ToggleVisibilityCommand extends BaseCommand {
const entityData = this.entityMap_.get(entityId);
if (!entityData) return;
entityData.visible = true;
- this.visibilityCallback_(entityId, true);
for (const childId of entityData.children) {
const childData = this.entityMap_.get(childId);
if (childData && childData.visible === false) {
diff --git a/editor/src/commands/index.ts b/editor/src/commands/index.ts
index c014f75e1..e08b1dba7 100644
--- a/editor/src/commands/index.ts
+++ b/editor/src/commands/index.ts
@@ -3,7 +3,7 @@
* @brief Command system exports
*/
-export { type Command, BaseCommand } from './Command';
+export { type Command, type ChangeEmitter, BaseCommand } from './Command';
export { PropertyCommand } from './PropertyCommand';
export {
CreateEntityCommand,
diff --git a/editor/src/panels/InspectorPanel.ts b/editor/src/panels/InspectorPanel.ts
index beae0d331..0417b2bba 100644
--- a/editor/src/panels/InspectorPanel.ts
+++ b/editor/src/panels/InspectorPanel.ts
@@ -4,7 +4,7 @@
*/
import type { Entity } from 'esengine';
-import type { EditorStore, AssetSelection } from '../store/EditorStore';
+import type { EditorStore, AssetSelection, DirtyFlag } from '../store/EditorStore';
import { getDefaultComponentData } from '../schemas/ComponentSchemas';
import { icons } from '../utils/icons';
import type { EditorInfo } from './inspector/InspectorHelpers';
@@ -30,7 +30,6 @@ export class InspectorPanel {
private store_: EditorStore;
private contentContainer_: HTMLElement;
private unsubscribe_: (() => void) | null = null;
- private unsubPropertyChange_: (() => void) | null = null;
private editors_: EditorInfo[] = [];
private currentEntity_: Entity | null = null;
private currentAssetPath_: string | null = null;
@@ -72,8 +71,7 @@ export class InspectorPanel {
this.lockBtn_ = this.container_.querySelector('.es-lock-btn');
this.setupLockButton();
- this.unsubscribe_ = store.subscribe(() => this.render());
- this.unsubPropertyChange_ = store.subscribeToPropertyChanges(() => this.updateEditors());
+ this.unsubscribe_ = store.subscribe((_state, dirtyFlags) => this.onStoreNotify(dirtyFlags));
this.render();
const pms = getPlayModeService();
@@ -134,16 +132,20 @@ export class InspectorPanel {
this.unsubscribe_();
this.unsubscribe_ = null;
}
- if (this.unsubPropertyChange_) {
- this.unsubPropertyChange_();
- this.unsubPropertyChange_ = null;
- }
}
// =========================================================================
// Main Render
// =========================================================================
+ private onStoreNotify(dirtyFlags?: ReadonlySet<DirtyFlag>): void {
+ if (dirtyFlags && dirtyFlags.size === 1 && dirtyFlags.has('property')) {
+ this.updateEditors();
+ return;
+ }
+ this.render();
+ }
+
private render(): void {
if (this.playMode_) return;
if (this.locked_) {
diff --git a/editor/src/store/EditorStore.ts b/editor/src/store/EditorStore.ts
index 693230d6e..242684aec 100644
--- a/editor/src/store/EditorStore.ts
+++ b/editor/src/store/EditorStore.ts
@@ -1,7 +1,7 @@
import type { Entity } from 'esengine';
import type { SceneData, EntityData, ComponentData } from '../types/SceneTypes';
import { createEmptyScene } from '../types/SceneTypes';
-import { CommandHistory } from '../commands';
+import { CommandHistory, type Command } from '../commands';
import { WorldTransformCache } from '../transform/WorldTransformCache';
import type { Transform } from '../math/Transform';
import { SelectionService } from './SelectionService';
@@ -72,11 +72,6 @@ export interface ComponentChangeEvent {
export type ComponentChangeListener = (event: ComponentChangeEvent) => void;
-const DERIVED_PROPERTY_MAP: Array<{ source: [string, string]; target: [string, string] }> = [
- { source: ['UIRect', 'size'], target: ['Sprite', 'size'] },
- { source: ['TextInput', 'backgroundColor'], target: ['Sprite', 'color'] },
-];
-
// =============================================================================
// EditorStore
// =============================================================================
@@ -483,6 +478,7 @@ export class EditorStore {
if (this.history_.undo()) {
this.state_.isDirty = true;
this.applyCommandSideEffects(cmd, true);
+ cmd.emitChangeEvents(this, true);
}
}
@@ -492,6 +488,7 @@ export class EditorStore {
if (this.history_.redo()) {
this.state_.isDirty = true;
this.applyCommandSideEffects(cmd, false);
+ cmd.emitChangeEvents(this, false);
}
}
@@ -544,21 +541,21 @@ export class EditorStore {
this.history_.execute(cmd);
this.state_.isDirty = true;
this.applyCommandSideEffects(cmd, false);
+ cmd.emitChangeEvents(this, false);
}
- private applyCommandSideEffects(cmd: import('../commands').Command, isUndo: boolean): void {
+ private applyCommandSideEffects(cmd: Command, isUndo: boolean): void {
cmd.updateEntityMap(this.entityMap_, isUndo);
if (cmd.structural) {
this.rebuildEntityMap();
this.worldTransforms_.setScene(this.state_.scene);
+ this.notifySceneSync();
+ this.notify('hierarchy');
} else {
this.worldTransforms_.invalidateAll();
+ this.notify('scene');
}
-
- this.syncDerivedProperties();
- this.notifySceneSync();
- this.notify(cmd.structural ? 'hierarchy' : 'scene');
}
private notifySceneSync(): void {
@@ -583,33 +580,11 @@ export class EditorStore {
}
notifyPropertyChange(event: PropertyChangeEvent): void {
- for (const rule of DERIVED_PROPERTY_MAP) {
- if (event.componentType === rule.source[0] && event.propertyName === rule.source[1]) {
- const entityData = this.entityMap_.get(event.entity);
- const targetComp = entityData?.components.find(c => c.type === rule.target[0]);
- if (targetComp) {
- targetComp.data[rule.target[1]] = event.newValue;
- }
- }
- }
-
for (const listener of this.propertyListeners_) {
listener(event);
}
}
- private syncDerivedProperties(): void {
- for (const entityData of this.entityMap_.values()) {
- for (const rule of DERIVED_PROPERTY_MAP) {
- const sourceComp = entityData.components.find(c => c.type === rule.source[0]);
- const targetComp = entityData.components.find(c => c.type === rule.target[0]);
- if (sourceComp && targetComp) {
- targetComp.data[rule.target[1]] = sourceComp.data[rule.source[1]];
- }
- }
- }
- }
-
notifyHierarchyChange(event: HierarchyChangeEvent): void {
for (const listener of this.hierarchyListeners_) {
listener(event);
diff --git a/editor/src/store/SceneOperations.ts b/editor/src/store/SceneOperations.ts
index a2f2c6706..169cff294 100644
--- a/editor/src/store/SceneOperations.ts
+++ b/editor/src/store/SceneOperations.ts
@@ -25,9 +25,6 @@ import type {
DirtyFlag,
PropertyChangeEvent,
VisibilityChangeEvent,
- HierarchyChangeEvent,
- EntityLifecycleEvent,
- ComponentChangeEvent,
} from './EditorStore';
export interface SceneOperationsHost {
@@ -42,9 +39,6 @@ export interface SceneOperationsHost {
notify(flag: DirtyFlag): void;
notifyPropertyChange(event: PropertyChangeEvent): void;
notifyVisibilityChange(event: VisibilityChangeEvent): void;
- notifyHierarchyChange(event: HierarchyChangeEvent): void;
- notifyEntityLifecycle(event: EntityLifecycleEvent): void;
- notifyComponentChange(event: ComponentChangeEvent): void;
selectEntity(entity: Entity | null): void;
worldTransforms_: {
updateEntity(entity: EntityData): void;
@@ -72,8 +66,6 @@ export class SceneOperations {
);
this.host_.executeCommand(cmd);
- this.host_.notifyEntityLifecycle({ entity: id, type: 'created', parent: parent as number | null });
-
if (parent !== null) {
const parentData = this.host_.entityMap_.get(parent as number);
if (parentData && !parentData.visible) {
@@ -103,21 +95,11 @@ export class SceneOperations {
if (hadSelection) {
this.host_.notify('selection');
}
-
- for (const id of descendants) {
- this.host_.notifyEntityLifecycle({ entity: id, type: 'deleted', parent: null });
- }
- this.host_.notifyEntityLifecycle({ entity: entity as number, type: 'deleted', parent: null });
}
deleteSelectedEntities(): void {
const toDelete = Array.from(this.host_.state_.selectedEntities);
- const descendantMap = new Map<number, number[]>();
- for (const id of toDelete) {
- descendantMap.set(id, this.collectDescendantIds(id));
- }
-
const commands = toDelete.map(id =>
new DeleteEntityCommand(this.host_.state_.scene, this.host_.entityMap_, id as Entity)
);
@@ -125,32 +107,16 @@ export class SceneOperations {
this.host_.executeCommand(compound);
this.host_.state_.selectedEntities.clear();
-
- for (const id of toDelete) {
- const descendants = descendantMap.get(id)!;
- for (const descId of descendants) {
- this.host_.notifyEntityLifecycle({ entity: descId, type: 'deleted', parent: null });
- }
- this.host_.notifyEntityLifecycle({ entity: id, type: 'deleted', parent: null });
- }
}
reparentEntity(entity: Entity, newParent: Entity | null): void {
const cmd = new ReparentCommand(this.host_.state_.scene, this.host_.entityMap_, entity, newParent);
this.host_.executeCommand(cmd);
- this.host_.notifyHierarchyChange({
- entity: entity as number,
- newParent: newParent as number | null,
- });
}
moveEntity(entity: Entity, newParent: Entity | null, index: number): void {
const cmd = new MoveEntityCommand(this.host_.state_.scene, this.host_.entityMap_, entity, newParent, index);
this.host_.executeCommand(cmd);
- this.host_.notifyHierarchyChange({
- entity: entity as number,
- newParent: newParent as number | null,
- });
}
renameEntity(entity: Entity, name: string): void {
@@ -179,8 +145,7 @@ export class SceneOperations {
const cmd = new ToggleVisibilityCommand(
this.host_.state_.scene,
this.host_.entityMap_,
- entityId,
- (id, visible) => this.host_.notifyVisibilityChange({ entity: id, visible })
+ entityId
);
this.host_.executeCommand(cmd);
@@ -196,8 +161,6 @@ export class SceneOperations {
if (this.isPrefabInstance(entity as number)) {
recordComponentAddedOverride(this.host_.state_.scene, entity as number, type, data);
}
-
- this.host_.notifyComponentChange({ entity: entity as number, componentType: type, action: 'added' });
}
removeComponent(entity: Entity, type: string): void {
@@ -209,7 +172,6 @@ export class SceneOperations {
const cmd = new RemoveComponentCommand(this.host_.state_.scene, this.host_.entityMap_, entity, type);
this.host_.executeCommand(cmd);
- this.host_.notifyComponentChange({ entity: entity as number, componentType: type, action: 'removed' });
}
reorderComponent(entity: Entity, fromIndex: number, toIndex: number): void {
@@ -247,14 +209,6 @@ export class SceneOperations {
newValue
);
}
-
- this.host_.notifyPropertyChange({
- entity: entity as number,
- componentType,
- propertyName,
- oldValue,
- newValue,
- });
}
updateProperties(
@@ -273,15 +227,6 @@ export class SceneOperations {
));
const compound = new CompoundCommand(commands, `Change ${componentType} properties`);
this.host_.executeCommand(compound);
- for (const c of changes) {
- this.host_.notifyPropertyChange({
- entity: entity as number,
- componentType,
- propertyName: c.property,
- oldValue: c.oldValue,
- newValue: c.newValue,
- });
- }
}
getComponent(entity: Entity, type: string): ComponentData | null {
@@ -318,6 +263,7 @@ export class SceneOperations {
oldValue,
newValue,
});
+ this.host_.notify('property');
}
isPrefabInstance(entityId: number): boolean {