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: extract walkParentChain helper and simplify UI hierarchy code
esengineb5964c666bad
diff --git a/sdk/src/ui/UIInteractionPlugin.ts b/sdk/src/ui/UIInteractionPlugin.tsindex 6ab33f0f2..b58591e51 100644--- a/sdk/src/ui/UIInteractionPlugin.ts+++ b/sdk/src/ui/UIInteractionPlugin.ts@@ -1,6 +1,6 @@import type { App, Plugin } from '../app';-import { registerComponent, WorldTransform, Sprite, Parent } from '../component';-import type { WorldTransformData, SpriteData, ParentData } from '../component';+import { registerComponent, WorldTransform, Sprite } from '../component';+import type { WorldTransformData, SpriteData } from '../component';import { defineSystem, Schedule } from '../system';import { Res } from '../resource';import { Input } from '../input';@@ -22,7 +22,7 @@ import type { UICameraData } from './UICameraInfo';import { UIMask } from './UIMask';import type { UIMaskData } from './UIMask';import { screenToWorld, pointInOBB, createInvVPCache } from './uiMath';-import { applyColorTransition, getEffectiveWidth, getEffectiveHeight, ensureComponent } from './uiHelpers';+import { applyColorTransition, getEffectiveWidth, getEffectiveHeight, ensureComponent, walkParentChain } from './uiHelpers';const vpCache = createInvVPCache();@@ -32,33 +32,28 @@ function isClippedByMask(worldMouseX: number,worldMouseY: number,): boolean {- let current = entity;- while (world.has(current, Parent)) {- const parentData = world.get(current, Parent) as ParentData;- const parentEntity = parentData.entity;- if (!world.valid(parentEntity)) break;-- if (world.has(parentEntity, UIMask)) {- const mask = world.get(parentEntity, UIMask) as UIMaskData;- if (mask.enabled && world.has(parentEntity, UIRect) && world.has(parentEntity, WorldTransform)) {- const wt = world.get(parentEntity, WorldTransform) as WorldTransformData;- const rect = world.get(parentEntity, UIRect) as UIRectData;- const maskW = getEffectiveWidth(rect) * wt.scale.x;- const maskH = getEffectiveHeight(rect) * wt.scale.y;- if (!pointInOBB(- worldMouseX, worldMouseY,- wt.position.x, wt.position.y,- maskW, maskH,- rect.pivot.x, rect.pivot.y,- wt.rotation.z, wt.rotation.w,- )) {- return true;- }- }+ let clipped = false;+ walkParentChain(world, entity, (ancestor) => {+ if (!world.has(ancestor, UIMask)) return false;+ const mask = world.get(ancestor, UIMask) as UIMaskData;+ if (!mask.enabled || !world.has(ancestor, UIRect) || !world.has(ancestor, WorldTransform)) return false;+ const wt = world.get(ancestor, WorldTransform) as WorldTransformData;+ const rect = world.get(ancestor, UIRect) as UIRectData;+ const maskW = getEffectiveWidth(rect) * wt.scale.x;+ const maskH = getEffectiveHeight(rect) * wt.scale.y;+ if (!pointInOBB(+ worldMouseX, worldMouseY,+ wt.position.x, wt.position.y,+ maskW, maskH,+ rect.pivot.x, rect.pivot.y,+ wt.rotation.z, wt.rotation.w,+ )) {+ clipped = true;+ return true;}- current = parentEntity;- }- return false;+ return false;+ });+ return clipped;}function emitWithBubbling(@@ -69,22 +64,14 @@ function emitWithBubbling(): void {events.emit(entity, type, entity);- let current = entity;- while (world.has(current, Parent)) {- const parentData = world.get(current, Parent) as ParentData;- const parentEntity = parentData.entity;- if (!world.valid(parentEntity)) break;- if (!world.has(parentEntity, Interactable)) {- current = parentEntity;- continue;- }- const interactable = world.get(parentEntity, Interactable) as InteractableData;+ walkParentChain(world, entity, (ancestor) => {+ if (!world.has(ancestor, Interactable)) return false;+ const interactable = world.get(ancestor, Interactable) as InteractableData;if (interactable.enabled) {- events.emit(parentEntity, type, entity);+ events.emit(ancestor, type, entity);}- if (interactable.blockRaycast) break;- current = parentEntity;- }+ return interactable.blockRaycast;+ });}export class UIInteractionPlugin implements Plugin {diff --git a/sdk/src/ui/UIMaskPlugin.ts b/sdk/src/ui/UIMaskPlugin.tsindex 1100bf0d4..40fa9e7b7 100644--- a/sdk/src/ui/UIMaskPlugin.ts+++ b/sdk/src/ui/UIMaskPlugin.ts@@ -3,12 +3,12 @@ import type { ESEngineModule, CppRegistry } from '../wasm';import type { MaskProcessorFn } from '../renderPipeline';import type { Entity } from '../types';import type { World } from '../world';-import { registerComponent, WorldTransform, Parent } from '../component';-import type { WorldTransformData, ParentData } from '../component';+import { registerComponent, WorldTransform } from '../component';+import type { WorldTransformData } from '../component';import { UIMask, type UIMaskData } from './UIMask';import { UIRect, type UIRectData } from './UIRect';import { intersectRects, quaternionToAngle2D, worldToScreen, type ScreenRect } from './uiMath';-import { getEffectiveWidth, getEffectiveHeight } from './uiHelpers';+import { getEffectiveWidth, getEffectiveHeight, walkParentChain } from './uiHelpers';function computeMaskScreenRect(world: World, entity: Entity,@@ -114,18 +114,16 @@ function processScissorMasks(const rootMasks: Entity[] = [];for (const entity of scissorMasks) {let isRoot = true;- let current = entity;- while (world.has(current, Parent)) {- const p = world.get(current, Parent) as ParentData;- if (maskSet.has(p.entity as number)) {- const parentMask = world.get(p.entity, UIMask) as UIMaskData;+ walkParentChain(world, entity, (ancestor) => {+ if (maskSet.has(ancestor as number)) {+ const parentMask = world.get(ancestor, UIMask) as UIMaskData;if (parentMask.mode === 'scissor') {isRoot = false;- break;+ return true;}}- current = p.entity;- }+ return false;+ });if (isRoot) rootMasks.push(entity);}@@ -174,15 +172,13 @@ function processStencilMasks(const rootMasks: Entity[] = [];for (const entity of stencilMasks) {let isRoot = true;- let current = entity;- while (world.has(current, Parent)) {- const p = world.get(current, Parent) as ParentData;- if (stencilMaskSet.has(p.entity as number)) {+ walkParentChain(world, entity, (ancestor) => {+ if (stencilMaskSet.has(ancestor as number)) {isRoot = false;- break;+ return true;}- current = p.entity;- }+ return false;+ });if (isRoot) rootMasks.push(entity);}diff --git a/sdk/src/ui/index.ts b/sdk/src/ui/index.tsindex 1eb87bdc5..06e08a3dc 100644--- a/sdk/src/ui/index.ts+++ b/sdk/src/ui/index.ts@@ -22,6 +22,7 @@ export {ensureComponent,syncFillSpriteSize,layoutChildEntity,+ walkParentChain,} from './uiHelpers';// Snapshot Utilitiesdiff --git a/sdk/src/ui/uiHelpers.ts b/sdk/src/ui/uiHelpers.tsindex 796fcb117..596f4c313 100644--- a/sdk/src/ui/uiHelpers.ts+++ b/sdk/src/ui/uiHelpers.ts@@ -221,6 +221,20 @@ export function syncFillSpriteSize(}}+export function walkParentChain(+ world: World, entity: Entity,+ callback: (ancestor: Entity) => boolean,+): void {+ let current = entity;+ while (world.has(current, Parent)) {+ const parentData = world.get(current, Parent) as ParentData;+ const parentEntity = parentData.entity;+ if (!world.valid(parentEntity)) break;+ if (callback(parentEntity)) return;+ current = parentEntity;+ }+}+export function ensureComponent(world: World, entity: Entity,component: AnyComponentDef, defaults?: Record<string, unknown>,