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: extract shared transform helpers to reduce duplication

esengine86112eb821e5
diff --git a/editor/src/math/Transform.ts b/editor/src/math/Transform.ts
index 14bf2f327..042df658f 100644
--- a/editor/src/math/Transform.ts
+++ b/editor/src/math/Transform.ts
@@ -169,3 +169,63 @@ export function cloneTransform(t: Transform): Transform {
scale: { ...t.scale },
};
}
+
+// =============================================================================
+// Entity Data Helpers
+// =============================================================================
+
+export interface EntityDataLike {
+ components: Array<{ type: string; data: Record<string, unknown> }>;
+ parent: number | null;
+ children: number[];
+}
+
+export function getLocalTransformFromEntity(entity: EntityDataLike): Transform {
+ const comp = entity.components.find(c => c.type === 'LocalTransform');
+ if (!comp) {
+ return createIdentityTransform();
+ }
+
+ return {
+ position: (comp.data.position as Vec3) ?? IDENTITY_TRANSFORM.position,
+ rotation: (comp.data.rotation as Quat) ?? IDENTITY_TRANSFORM.rotation,
+ scale: (comp.data.scale as Vec3) ?? IDENTITY_TRANSFORM.scale,
+ };
+}
+
+export interface UIRectLike {
+ size: { x: number; y: number };
+ anchor: { x: number; y: number };
+ pivot: { x: number; y: number };
+}
+
+const DEFAULT_UIRECT: UIRectLike = {
+ size: { x: 100, y: 100 },
+ anchor: { x: 0.5, y: 0.5 },
+ pivot: { x: 0.5, y: 0.5 },
+};
+
+export function getUIRectFromEntity(entity: EntityDataLike): UIRectLike | null {
+ const comp = entity.components.find(c => c.type === 'UIRect');
+ if (!comp) return null;
+
+ return {
+ size: (comp.data.size as { x: number; y: number }) ?? DEFAULT_UIRECT.size,
+ anchor: (comp.data.anchor as { x: number; y: number }) ?? DEFAULT_UIRECT.anchor,
+ pivot: (comp.data.pivot as { x: number; y: number }) ?? DEFAULT_UIRECT.pivot,
+ };
+}
+
+export function getEntitySize(entity: EntityDataLike): { x: number; y: number } {
+ const uiRect = getUIRectFromEntity(entity);
+ if (uiRect?.size) {
+ return uiRect.size;
+ }
+
+ const sprite = entity.components.find(c => c.type === 'Sprite');
+ if (sprite?.data?.size) {
+ return sprite.data.size as { x: number; y: number };
+ }
+
+ return { x: 100, y: 100 };
+}
diff --git a/editor/src/transform/WorldTransformCache.ts b/editor/src/transform/WorldTransformCache.ts
index 5f0dd5ec3..9c322e846 100644
--- a/editor/src/transform/WorldTransformCache.ts
+++ b/editor/src/transform/WorldTransformCache.ts
@@ -8,6 +8,7 @@ import {
Transform,
composeTransforms,
createIdentityTransform,
+ getLocalTransformFromEntity,
} from '../math/Transform';
// =============================================================================
@@ -125,7 +126,7 @@ export class WorldTransformCache {
return createIdentityTransform();
}
- const localTransform = this.getLocalTransform(entity);
+ const localTransform = getLocalTransformFromEntity(entity);
if (entity.parent === null) {
return localTransform;
@@ -134,17 +135,4 @@ export class WorldTransformCache {
const parentWorld = this.getWorldTransform(entity.parent);
return composeTransforms(parentWorld, localTransform);
}
-
- private getLocalTransform(entity: EntityData): Transform {
- const comp = entity.components.find(c => c.type === 'LocalTransform');
- if (!comp) {
- return createIdentityTransform();
- }
-
- return {
- position: (comp.data.position as any) ?? { x: 0, y: 0, z: 0 },
- rotation: (comp.data.rotation as any) ?? { x: 0, y: 0, z: 0, w: 1 },
- scale: (comp.data.scale as any) ?? { x: 1, y: 1, z: 1 },
- };
- }
}