Files
esengine/packages/rendering/fairygui/src/layout/Relations.ts
YHH 155411e743 refactor: reorganize package structure and decouple framework packages (#338)
* refactor: reorganize package structure and decouple framework packages

## Package Structure Reorganization
- Reorganized 55 packages into categorized subdirectories:
  - packages/framework/ - Generic framework (Laya/Cocos compatible)
  - packages/engine/ - ESEngine core modules
  - packages/rendering/ - Rendering modules (WASM dependent)
  - packages/physics/ - Physics modules
  - packages/streaming/ - World streaming
  - packages/network-ext/ - Network extensions
  - packages/editor/ - Editor framework and plugins
  - packages/rust/ - Rust WASM engine
  - packages/tools/ - Build tools and SDK

## Framework Package Decoupling
- Decoupled behavior-tree and blueprint packages from ESEngine dependencies
- Created abstracted interfaces (IBTAssetManager, IBehaviorTreeAssetContent)
- ESEngine-specific code moved to esengine/ subpath exports
- Framework packages now usable with Cocos/Laya without ESEngine

## CI Configuration
- Updated CI to only type-check and lint framework packages
- Added type-check:framework and lint:framework scripts

## Breaking Changes
- Package import paths changed due to directory reorganization
- ESEngine integrations now use subpath imports (e.g., '@esengine/behavior-tree/esengine')

* fix: update es-engine file path after directory reorganization

* docs: update README to focus on framework over engine

* ci: only build framework packages, remove Rust/WASM dependencies

* fix: remove esengine subpath from behavior-tree and blueprint builds

ESEngine integration code will only be available in full engine builds.
Framework packages are now purely engine-agnostic.

* fix: move network-protocols to framework, build both in CI

* fix: update workflow paths from packages/core to packages/framework/core

* fix: exclude esengine folder from type-check in behavior-tree and blueprint

* fix: update network tsconfig references to new paths

* fix: add test:ci:framework to only test framework packages in CI

* fix: only build core and math npm packages in CI

* fix: exclude test files from CodeQL and fix string escaping security issue
2025-12-26 14:50:35 +08:00

185 lines
4.8 KiB
TypeScript

import { ERelationType } from '../core/FieldTypes';
import type { GObject } from '../core/GObject';
import type { GComponent } from '../core/GComponent';
import type { ByteBuffer } from '../utils/ByteBuffer';
import { RelationItem } from './RelationItem';
/**
* Relations
*
* Manages constraint-based layout relationships between UI objects.
*
* 管理 UI 对象之间的约束布局关系
*/
export class Relations {
/** Owner object | 所有者对象 */
public readonly owner: GObject;
/** Size dirty flag | 尺寸脏标记 */
public sizeDirty: boolean = false;
private _items: RelationItem[] = [];
constructor(owner: GObject) {
this.owner = owner;
}
/**
* Add a relation
* 添加关联
*/
public add(target: GObject, relationType: ERelationType, bUsePercent: boolean = false): void {
let item: RelationItem | null = null;
for (const existing of this._items) {
if (existing.target === target) {
item = existing;
break;
}
}
if (!item) {
item = new RelationItem(this.owner);
item.target = target;
this._items.push(item);
}
item.add(relationType, bUsePercent);
}
/**
* Remove a relation
* 移除关联
*/
public remove(target: GObject, relationType: ERelationType = ERelationType.Size): void {
for (let i = this._items.length - 1; i >= 0; i--) {
const item = this._items[i];
if (item.target === target) {
item.remove(relationType);
if (item.isEmpty()) {
this._items.splice(i, 1);
}
break;
}
}
}
/**
* Check if target has any relations
* 检查目标是否有任何关联
*/
public contains(target: GObject): boolean {
return this._items.some(item => item.target === target);
}
/**
* Clear relations with a target
* 清除与目标的所有关联
*/
public clearFor(target: GObject): void {
for (let i = this._items.length - 1; i >= 0; i--) {
if (this._items[i].target === target) {
this._items.splice(i, 1);
}
}
}
/**
* Clear all relations
* 清除所有关联
*/
public clearAll(): void {
for (const item of this._items) {
item.dispose();
}
this._items.length = 0;
}
/**
* Copy relations from another object
* 从另一个对象复制关联
*/
public copyFrom(source: Relations): void {
this.clearAll();
for (const item of source._items) {
const newItem = new RelationItem(this.owner);
newItem.copyFrom(item);
this._items.push(newItem);
}
}
/**
* Called when owner size changed
* 当所有者尺寸改变时调用
*/
public onOwnerSizeChanged(dWidth: number, dHeight: number, bApplyPivot: boolean): void {
for (const item of this._items) {
item.applyOnSelfResized(dWidth, dHeight, bApplyPivot);
}
}
/**
* Ensure relations size is correct
* 确保关联尺寸正确
*/
public ensureRelationsSizeCorrect(): void {
if (!this.sizeDirty) return;
this.sizeDirty = false;
for (const item of this._items) {
item.target?.ensureSizeCorrect();
}
}
/**
* Get items count
* 获取项目数量
*/
public get count(): number {
return this._items.length;
}
/**
* Setup relations from buffer
* 从缓冲区设置关联
*/
public setup(buffer: ByteBuffer, bParentToChild: boolean): void {
const cnt = buffer.readByte();
for (let i = 0; i < cnt; i++) {
const targetIndex = buffer.getInt16();
let target: GObject | null = null;
if (targetIndex === -1) {
target = this.owner.parent;
} else if (bParentToChild) {
target = (this.owner as GComponent).getChildAt(targetIndex);
} else if (this.owner.parent) {
target = this.owner.parent.getChildAt(targetIndex);
}
if (!target) continue;
const newItem = new RelationItem(this.owner);
newItem.target = target;
this._items.push(newItem);
const cnt2 = buffer.readByte();
for (let j = 0; j < cnt2; j++) {
const rt = buffer.readByte() as ERelationType;
const bUsePercent = buffer.readBool();
newItem.internalAdd(rt, bUsePercent);
}
}
}
/**
* Dispose
* 销毁
*/
public dispose(): void {
this.clearAll();
}
}