2025-02-20 11:27:28 +08:00
|
|
|
/**
|
|
|
|
* @Author: Gongxh
|
|
|
|
* @Date: 2024-12-26
|
|
|
|
* @Description: 自定义组件扩展帮助类
|
|
|
|
*/
|
|
|
|
import { UIObjectFactory } from "fairygui-cc";
|
|
|
|
import { debug } from "../tool/log";
|
|
|
|
import { PropsHelper } from "./PropsHelper";
|
|
|
|
import { _uidecorator } from "./UIDecorator";
|
|
|
|
|
|
|
|
export class ComponentExtendHelper {
|
2025-07-15 11:36:47 +08:00
|
|
|
/** 已注册的组件集合 @internal */
|
|
|
|
private static _registeredComponents: Set<string> = new Set<string>();
|
|
|
|
|
2025-02-20 11:27:28 +08:00
|
|
|
public static register(): void {
|
|
|
|
for (const { ctor, res } of _uidecorator.getComponentMaps().values()) {
|
2025-07-15 11:36:47 +08:00
|
|
|
const componentKey = `${res.pkg}/${res.name}`;
|
|
|
|
if (this._registeredComponents.has(componentKey)) {
|
|
|
|
debug(`自定义组件已注册,跳过 组件名:${res.name} 包名:${res.pkg}`);
|
|
|
|
continue;
|
|
|
|
}
|
2025-02-20 11:27:28 +08:00
|
|
|
debug(`自定义组件注册 组件名:${res.name} 包名:${res.pkg}`);
|
|
|
|
this.registerComponent(ctor, res.pkg, res.name);
|
2025-07-15 11:36:47 +08:00
|
|
|
this._registeredComponents.add(componentKey);
|
2025-02-20 11:27:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-07-22 17:08:02 +08:00
|
|
|
/**
|
|
|
|
* 动态注册自定义组件
|
|
|
|
* @param ctor 组件构造函数
|
|
|
|
* @param pkg 包名
|
|
|
|
* @param name 组件名
|
|
|
|
*/
|
|
|
|
public static dynamicRegister(ctor: any, pkg: string, name: string): void {
|
|
|
|
const componentKey = `${pkg}/${name}`;
|
|
|
|
if (this._registeredComponents.has(componentKey)) {
|
|
|
|
debug(`自定义组件已注册,跳过 组件名:${name} 包名:${pkg}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
debug(`自定义组件注册 组件名:${name} 包名:${pkg}`);
|
|
|
|
this.registerComponent(ctor, pkg, name);
|
|
|
|
this._registeredComponents.add(componentKey);
|
|
|
|
}
|
|
|
|
|
2025-02-20 11:27:28 +08:00
|
|
|
/**
|
|
|
|
* 注册自定义组件信息
|
|
|
|
* @param info
|
2025-03-07 16:02:00 +08:00
|
|
|
* @internal
|
2025-02-20 11:27:28 +08:00
|
|
|
*/
|
|
|
|
private static registerComponent(ctor: any, pkg: string, name: string): void {
|
|
|
|
// 自定义组件扩展
|
|
|
|
const onConstruct = function (this: any): void {
|
2025-06-30 15:17:25 +08:00
|
|
|
PropsHelper.serializeProps(this, pkg, name);
|
2025-02-20 11:27:28 +08:00
|
|
|
this.onInit && this.onInit();
|
|
|
|
};
|
|
|
|
ctor.prototype.onConstruct = onConstruct;
|
|
|
|
// 自定义组件扩展
|
|
|
|
UIObjectFactory.setExtension(`ui://${pkg}/${name}`, ctor);
|
|
|
|
}
|
|
|
|
}
|