Skip to content

Commit d2cf62d

Browse files
committed
refactor: extract getSizeComponentType helper and optimize scene sync
- Add getSizeComponentType() to reduce duplicate UIRect/Sprite lookup logic - Move hasText check inside Sprite case to avoid unnecessary iteration - Change pending scene handling from while loop to single if statement
1 parent 084ffc2 commit d2cf62d

2 files changed

Lines changed: 52 additions & 9 deletions

File tree

editor/src/panels/SceneViewPanel.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ export class SceneViewPanel {
117117
private settingsDropdown_: HTMLElement | null = null;
118118
private settingsDropdownClickHandler_: ((e: MouseEvent) => void) | null = null;
119119

120+
private getSizeComponentType(entityData: import('../types/SceneTypes').EntityData): 'UIRect' | 'Sprite' | null {
121+
if (entityData.components.some(c => c.type === 'UIRect')) return 'UIRect';
122+
if (entityData.components.some(c => c.type === 'Sprite')) return 'Sprite';
123+
return null;
124+
}
125+
120126
constructor(container: HTMLElement, store: EditorStore, options?: SceneViewPanelOptions) {
121127
this.container_ = container;
122128
this.store_ = store;
@@ -752,9 +758,9 @@ export class SceneViewPanel {
752758
newWidth = Math.max(1, newWidth);
753759
newHeight = Math.max(1, newHeight);
754760

755-
const sprite = entityData.components.find(c => c.type === 'Sprite');
756-
if (sprite) {
757-
this.store_.updatePropertyDirect(entity, 'Sprite', 'size', { x: newWidth, y: newHeight });
761+
const sizeType = this.getSizeComponentType(entityData);
762+
if (sizeType) {
763+
this.store_.updatePropertyDirect(entity, sizeType, 'size', { x: newWidth, y: newHeight });
758764
}
759765

760766
const transform = entityData.components.find(c => c.type === 'LocalTransform');
@@ -777,13 +783,14 @@ export class SceneViewPanel {
777783
const entityData = this.store_.getSelectedEntityData();
778784
if (!entityData) return;
779785

780-
const sprite = entityData.components.find(c => c.type === 'Sprite');
781-
if (sprite && this.rectDragOriginalSize_) {
782-
const currentSize = sprite.data.size as { x: number; y: number };
786+
const sizeType = this.getSizeComponentType(entityData);
787+
if (sizeType && this.rectDragOriginalSize_) {
788+
const comp = entityData.components.find(c => c.type === sizeType)!;
789+
const currentSize = comp.data.size as { x: number; y: number };
783790
if (currentSize && !this.valuesEqual(this.rectDragOriginalSize_, currentSize)) {
784791
this.store_.updateProperty(
785792
entity,
786-
'Sprite',
793+
sizeType,
787794
'size',
788795
{ ...this.rectDragOriginalSize_ },
789796
{ ...currentSize }

editor/src/renderer/EditorSceneRenderer.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ export class EditorSceneRenderer {
3636
/** Map entity ID to EntityData for fast lookup */
3737
private entityDataMap_: Map<number, EntityData> = new Map();
3838

39+
/** Sync lock to prevent concurrent syncScene calls */
40+
private syncing_ = false;
41+
private pendingScene_: SceneData | null = null;
42+
3943
constructor() {
4044
this.camera_ = new EditorCamera();
4145
}
@@ -76,6 +80,29 @@ export class EditorSceneRenderer {
7680
async syncScene(scene: SceneData): Promise<void> {
7781
if (!this.registry_ || !this.module_) return;
7882

83+
if (this.syncing_) {
84+
this.pendingScene_ = scene;
85+
return;
86+
}
87+
88+
this.syncing_ = true;
89+
90+
try {
91+
await this.syncSceneInternal(scene);
92+
93+
if (this.pendingScene_) {
94+
const latest = this.pendingScene_;
95+
this.pendingScene_ = null;
96+
await this.syncSceneInternal(latest);
97+
}
98+
} finally {
99+
this.syncing_ = false;
100+
}
101+
}
102+
103+
private async syncSceneInternal(scene: SceneData): Promise<void> {
104+
if (!this.registry_ || !this.module_) return;
105+
79106
this.clearRegistry();
80107

81108
this.sceneData_ = scene;
@@ -210,6 +237,10 @@ export class EditorSceneRenderer {
210237
this.registry_.destroy(entity);
211238
}
212239
this.entityMap_.clear();
240+
241+
if (this.textRenderer_) {
242+
this.textRenderer_.releaseAll();
243+
}
213244
}
214245

215246
private async syncComponent(entity: Entity, comp: ComponentData, entityId: number): Promise<void> {
@@ -220,9 +251,14 @@ export class EditorSceneRenderer {
220251
this.syncTransform(entity, comp.data, entityId);
221252
break;
222253

223-
case 'Sprite':
224-
await this.syncSprite(entity, comp.data);
254+
case 'Sprite': {
255+
const entityData = this.entityDataMap_.get(entityId);
256+
const hasText = entityData?.components.some(c => c.type === 'Text');
257+
if (!hasText) {
258+
await this.syncSprite(entity, comp.data);
259+
}
225260
break;
261+
}
226262

227263
case 'Camera':
228264
this.syncCamera(entity, comp.data);

0 commit comments

Comments
 (0)